Docs Home → MongoDB Manual To index a field that holds an array value, MongoDB creates an index key for each element in the array. These multikey indexes support efficient queries against array fields. Multikey indexes can be constructed over arrays that hold both scalar values [1] (e.g. strings, numbers) and nested documents. To create a multikey index, use the
MongoDB automatically creates a multikey index if any indexed field is an array; you do not need to explicitly specify the multikey type. Changed in version 3.4: For the WiredTiger and In-Memory storage engines only, Starting in MongoDB 3.4, for multikey indexes created using MongoDB 3.4 or later, MongoDB keeps track of which indexed field or fields cause an index to be a multikey index. Tracking this information allows the MongoDB query engine to use tighter index bounds. If an index is multikey, then computation of the index bounds follows special rules. For details on multikey index bounds, see Multikey Index Bounds. For unique indexes, the unique constraint applies across separate documents in the collection rather than within a single document. Because the unique constraint applies to separate documents, for a unique multikey index, a document may have array elements that result in repeating index key values as long as the index key values for that document do not duplicate those of another document. For more information, see Unique Constraint Across Separate Documents. For a compound multikey index, each indexed document can have at most one indexed field whose value is an array. That is:
If a field is an array of documents, you can index the embedded fields to create a compound index. For example, consider a collection that contains the following documents:
You can create a compound index on For an example, see Index Arrays with Embedded Documents. TipAs a result of changes to sorting behavior on array fields in MongoDB 4.4, when you sort on an array indexed with a multikey index, the query plan includes a blocking sort stage, unless:
You cannot specify a multikey index as the shard key index. However, if the shard key index is a prefix of a compound index, the compound index is allowed to become a compound multikey index if one of the other keys (i.e. keys that are not part of the shard key) indexes an array. Compound multikey indexes can have an impact on performance. Hashed indexes cannot be multikey. Multikey indexes cannot cover queries over array field(s). However, starting in 3.6, multikey indexes can cover queries over the non-array fields if the index tracks which field or fields cause the index to be multikey. Multikey indexes created in MongoDB 3.4 or later on storage engines other than MMAPv1 [3] track this data. When a query filter specifies an exact match for an array as a whole, MongoDB can use the multikey index to look up the first element of the query array but cannot use the multikey index scan to find the whole array. Instead, after using the multikey index to look up the first element of the query array, MongoDB retrieves the associated documents and filters for documents whose array matches the array in the query. For example, consider an
The collection
has a multikey index on the
The following query looks for documents where the
MongoDB can use the multikey index to find documents that have
Indexes built on MongoDB 3.2 or earlier do not contain the necessary flags to support optimized multikey index use. To benefit from the performance enhancements of multikey indexes, you must either:
Create a
Create an
index on the field
Since the
You can create multikey indexes on array fields that contain nested objects. Consider an
The following operation creates a multikey index on the
The compound multikey index can support queries with predicates that include both indexed fields as well as predicates that include only the index prefix
For details on how MongoDB can combine multikey index bounds, see Multikey Index Bounds. For more information on behavior of compound indexes and prefixes, see compound indexes and prefixes. The compound multikey index can also support sort operations, such as the following examples:
For more information on behavior of compound indexes and sort operations, see Use Indexes to Sort Query Results. |