Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect. Indexes are special data structures [1] that store a small portion of the collection's data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations. In addition, MongoDB can return sorted results by using the ordering in the index. The following diagram illustrates a query that selects and orders the matching documents using an index: Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection. MongoDB creates a unique index on the
_id field during the creation of a collection. The NoteIn sharded
clusters, if you do not use the ➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page. The default name for an index is the concatenation of the indexed keys and each key's direction in the index ( i.e. 1 or -1) using underscores as a separator. For example, an index created on You can create indexes with a custom name, such as one that is more human-readable than the
default. For example, consider an application that frequently queries the
You can view index names using the
MongoDB provides a number of different index types to support specific types of data and queries. In addition to the MongoDB-defined For a single-field index and sort operations, the sort order (i.e. ascending or descending) of the index key does not matter because MongoDB can traverse the index in either direction. See Single Field Indexes and Sort with a Single Field Index for more information on single-field indexes. MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes. The order of fields listed in a compound index has significance. For instance, if a compound index consists of For compound indexes and sort operations, the sort order (i.e. ascending or descending) of the index keys can determine whether the index can support a sort operation. See Sort Order for more information on the impact of index order on results in compound indexes. See also:
MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays. MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type. See Multikey Indexes and Multikey Index Bounds for more information on multikey indexes. To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes: 2d indexes that uses planar geometry when returning results and 2dsphere indexes that use spherical geometry to return results. See MongoDB provides a See Text Indexes for more information on text indexes and search. To support hash based sharding, MongoDB provides a hashed index type, which indexes the hash of the value of a field. These indexes have a more random distribution of values along their range, but only support equality matches and cannot support range-based queries. Starting in MongoDB 5.3, you can create a collection with a clustered index. Collections created with a clustered index are called clustered collections. See Clustered Collections. The unique property for an index causes MongoDB to reject duplicate values for the indexed field. Other than the unique constraint, unique indexes are functionally interchangeable with other MongoDB indexes. Partial indexes only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance. Partial indexes offer a superset of the functionality of sparse indexes and should be preferred over sparse indexes. The sparse property of an index ensures that the index only contain entries for documents that have the indexed field. The index skips documents that do not have the indexed field. You can combine the sparse index option with the unique index option to prevent inserting documents that have duplicate values for the indexed field(s) and skip indexing documents that lack the indexed field(s). TTL indexes are special indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time. This is ideal for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time. See: Expire Data from Collections by Setting TTL for implementation instructions. New in version 4.4. Hidden indexes are not visible to the query planner and cannot be used to support a query. By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index. If the impact is negative, the user can unhide the index instead of having to recreate a dropped index. And because indexes are fully maintained while hidden, the indexes are immediately available for use once unhidden. Except for the Indexes can improve the efficiency of read operations. The Analyze Query Performance tutorial provides an example of the execution statistics of a query with and without an index. For information on how MongoDB chooses an index to use, see query optimizer. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. To use an index for string comparisons, an operation must also specify the same collation. That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation. For example, the collection
The following query operation, which specifies the same collation as the index, can use the index:
However, the following query operation, which by default uses the "simple" binary collator, cannot use the index:
For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys. For example, the collection
The following operations, which use
The following operation, which uses
For more information on collation, see the collation reference page. The following indexes only support simple binary comparison and do not support collation:
When the query criteria and the projection of a query include only the indexed fields, MongoDB returns results directly from the index without scanning any documents or bringing documents into memory. These covered queries can be very efficient. For more information on covered queries, see Covered Query. MongoDB can use the intersection of indexes to fulfill queries. For queries that specify compound query conditions, if one index can fulfill a part of a query condition, and another index can fulfill another part of the query condition, then MongoDB can use the intersection of the two indexes to fulfill the query. Whether the use of a compound index or the use of an index intersection is more efficient depends on the particular query and the system. For details on index intersection, see Index Intersection. Certain restrictions apply to indexes, such as the length of the index keys or the number of indexes per collection. See Index Limitations for details. Although indexes can improve query performances, indexes also present some operational considerations. See Operational Considerations for Indexes for more information. Applications may encounter reduced performance during index builds, including limited read/write access to the collection. For more information on the index build process, see Index Builds on Populated Collections, including the Index Builds in Replicated Environments section. Some drivers may specify indexes, using What does a search index do?A search index helps users quickly find information on a website. It is designed to map search queries to documents or URLs that might appear in the results.
How does search work in MongoDB?MongoDB text search uses the Snowball stemming library to reduce words to an expected root form (or stem) based on common language rules. Algorithmic stemming provides a quick reduction, but languages have exceptions (such as irregular or contradicting verb conjugation patterns) that can affect accuracy.
What are indexes in MongoDB Atlas?Indexes support the efficient execution of queries in MongoDB and should be considered for fields which your application reads often.
|