- Indexes >
- Multikey Indexes
Multikey Indexes¶
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.
[1] | A scalar value refers to value that is neither an embedded document nor an array. |
Create Multikey Index¶
To create a multikey index, use the
db.collection.createIndex()
method:
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.
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.
Unique Multikey Index¶
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.
Limitations¶
Compound Multikey Indexes¶
For a compound multikey index, each indexed document can have at most one indexed field whose value is an array. That is:
You cannot create a compound multikey index if more than one to-be-indexed field of a document is an array. For example, consider a collection that contains the following document:
You cannot create a compound multikey index
{ a: 1, b: 1 }
on the collection since both thea
andb
fields are arrays.Or, if a compound multikey index already exists, you cannot insert a document that would violate this restriction.
Consider a collection that contains the following documents:
A compound multikey index
{ a: 1, b: 1 }
is permissible since for each document, only one field indexed by the compound multikey index is an array; i.e. no document contains array values for botha
andb
fields.However, after creating the compound multikey index, if you attempt to insert a document where both
a
andb
fields are arrays, MongoDB will fail the insert.
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 { "a.x": 1, "a.z": 1 }
. The
restriction where at most one indexed field can be an array also
applies.
For an example, see Index Arrays with Embedded Documents.
Sorting¶
As a result of changes to sorting behavior on array fields in MongoDB 3.6, when sorting on an array indexed with a multikey index the query plan includes a blocking SORT stage. The new sorting behavior may negatively impact performance.
In a blocking SORT, all input must be consumed by the sort step before it can produce output. In a non-blocking, or indexed sort, the sort step scans the index to produce results in the requested order.
Shard Keys¶
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.
Covered Queries¶
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 [2] track this data.
[2] | Starting in version 4.2, MongoDB removes the deprecated MMAPv1 storage engine. |
Query on the Array Field as a Whole¶
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 inventory
collection that contains the
following documents:
The collection has a multikey index on the ratings
field:
The following query looks for documents where the ratings
field is
the array [ 5, 9 ]
:
MongoDB can use the multikey index to find documents that have 5
at
any position in the ratings
array. Then, MongoDB retrieves these
documents and filters for documents whose ratings
array equals the
query array [ 5, 9 ]
.
Examples¶
Index Basic Arrays¶
Consider a survey
collection with the following document:
Create an index on the field ratings
:
Since the ratings
field contains an array, the index on ratings
is multikey. The multikey index contains the following three index
keys, each pointing to the same document:
2
,5
, and9
.
Index Arrays with Embedded Documents¶
You can create multikey indexes on array fields that contain nested objects.
Consider an inventory
collection with documents of the following
form:
The following operation creates a multikey index on the stock.size
and stock.quantity
fields:
The compound multikey index can support queries with predicates that
include both indexed fields as well as predicates that include only the
index prefix "stock.size"
, as in the following examples:
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.