Multikey Indexes
To index a field that holds an array value, MongoDB creates an indexkey for each element in the array. These multikey indexes supportefficient queries against array fields. Multikey indexes can beconstructed 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 documentnor an array. |
Create Multikey Index
To create a multikey index, use thedb.collection.createIndex()
method:
- db.coll.createIndex( { <field>: < 1 or -1 > } )
MongoDB automatically creates a multikey index if any indexed field isan 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 MongoDB3.4 or later, MongoDB keeps track of which indexed field or fieldscause an index to be a multikey index. Tracking this informationallows the MongoDB query engine to use tighter index bounds.
Index Bounds
If an index is multikey, then computation of the index bounds followsspecial rules. For details on multikey index bounds, seeMultikey Index Bounds.
Unique Multikey Index
For unique indexes, the unique constraintapplies across separate documents in the collection rather than withina single document.
Because the unique constraint applies to separate documents, for aunique multikey index, a documentmay have array elements that result in repeating index key values aslong as the index key values for that document do not duplicate thoseof another document.
For more information, see Unique Constraint Across Separate Documents.
Limitations
Compound Multikey Indexes
For a compound multikey index, eachindexed document can have at most one indexed field whose value is anarray. That is:
- You cannot create a compound multikey index if more than oneto-be-indexed field of a document is an array. For example, considera collection that contains the following document:
- { _id: 1, a: [ 1, 2 ], b: [ 1, 2 ], category: "AB - both arrays" }
You cannot create a compound multikey index { a: 1, b: 1 }
on thecollection since both the a
and b
fields are arrays.
- Or, if a compound multikey index already exists, you cannot insert adocument that would violate this restriction.
Consider a collection that contains the following documents:
- { _id: 1, a: [1, 2], b: 1, category: "A array" }
- { _id: 2, a: 1, b: [1, 2], category: "B array" }
A compound multikey index { a: 1, b: 1 }
is permissible since foreach document, only one field indexed by the compound multikey indexis an array; i.e. no document contains array values for both a
and b
fields.
However, after creating the compound multikey index, if you attemptto insert a document where both a
and b
fields are arrays,MongoDB will fail the insert.
If a field is an array of documents, you can index the embedded fieldsto create a compound index. For example, consider a collectionthat contains the following documents:
- { _id: 1, a: [ { x: 5, z: [ 1, 2 ] }, { z: [ 1, 2 ] } ] }
- { _id: 2, a: [ { x: 5 }, { z: 4 } ] }
You can create a compound index on { "a.x": 1, "a.z": 1 }
. Therestriction where at most one indexed field can be an array alsoapplies.
For an example, see Index Arrays with Embedded Documents.
See also
Sorting
As a result of changes to sorting behavior on array fields in MongoDB3.6, when sorting on an array indexed with amultikey index the query plan includesa blocking SORT stage. The new sorting behavior may negatively impactperformance.
In a blocking SORT, all input must be consumed by the sort step beforeit can produce output. In a non-blocking, or indexed sort, thesort 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 isallowed 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
Hashed indexes cannot be multikey.
Covered Queries
Multikey indexes cannot cover queriesover array field(s).
However, starting in 3.6, multikey indexes can cover queries over thenon-array fields if the index tracks which field or fields cause theindex to be multikey. Multikey indexes created in MongoDB 3.4 or lateron storage engines other than MMAPv1[#]_ 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 awhole, MongoDB can use the multikey index to lookup the first element of the query array but cannot use the multikeyindex scan to find the whole array. Instead, after using the multikeyindex to look up the first element of the query array, MongoDBretrieves the associated documents and filters for documents whosearray matches the array in the query.
For example, consider an inventory
collection that contains thefollowing documents:
- { _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] }
- { _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] }
- { _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] }
- { _id: 8, type: "food", item: "ddd", ratings: [ 9, 5 ] }
- { _id: 9, type: "food", item: "eee", ratings: [ 5, 9, 5 ] }
The collection has a multikey index on the ratings
field:
- db.inventory.createIndex( { ratings: 1 } )
The following query looks for documents where the ratings
field isthe array [ 5, 9 ]
:
- db.inventory.find( { ratings: [ 5, 9 ] } )
MongoDB can use the multikey index to find documents that have 5
atany position in the ratings
array. Then, MongoDB retrieves thesedocuments and filters for documents whose ratings
array equals thequery array [ 5, 9 ]
.
$expr
$expr
does not support multikey indexes.
Examples
Index Basic Arrays
Consider a survey
collection with the following document:
- { _id: 1, item: "ABC", ratings: [ 2, 5, 9 ] }
Create an index on the field ratings
:
- db.survey.createIndex( { ratings: 1 } )
Since the ratings
field contains an array, the index on ratings
is multikey. The multikey index contains the following three indexkeys, each pointing to the same document:
2
,5
, and9
.
Index Arrays with Embedded Documents
You can create multikey indexes on array fields that contain nestedobjects.
Consider an inventory
collection with documents of the followingform:
- {
- _id: 1,
- item: "abc",
- stock: [
- { size: "S", color: "red", quantity: 25 },
- { size: "S", color: "blue", quantity: 10 },
- { size: "M", color: "blue", quantity: 50 }
- ]
- }
- {
- _id: 2,
- item: "def",
- stock: [
- { size: "S", color: "blue", quantity: 20 },
- { size: "M", color: "blue", quantity: 5 },
- { size: "M", color: "black", quantity: 10 },
- { size: "L", color: "red", quantity: 2 }
- ]
- }
- {
- _id: 3,
- item: "ijk",
- stock: [
- { size: "M", color: "blue", quantity: 15 },
- { size: "L", color: "blue", quantity: 100 },
- { size: "L", color: "red", quantity: 25 }
- ]
- }
- ...
The following operation creates a multikey index on the stock.size
and stock.quantity
fields:
- db.inventory.createIndex( { "stock.size": 1, "stock.quantity": 1 } )
The compound multikey index can support queries with predicates thatinclude both indexed fields as well as predicates that include only theindex prefix "stock.size"
, as in the following examples:
- db.inventory.find( { "stock.size": "M" } )
- db.inventory.find( { "stock.size": "S", "stock.quantity": { $gt: 20 } } )
For details on how MongoDB can combine multikey index bounds, seeMultikey Index Bounds. For more information on behavior ofcompound indexes and prefixes, see compound indexes and prefixes.
The compound multikey index can also support sort operations, such asthe following examples:
- db.inventory.find( ).sort( { "stock.size": 1, "stock.quantity": 1 } )
- db.inventory.find( { "stock.size": "M" } ).sort( { "stock.quantity": 1 } )
For more information on behavior of compound indexes and sortoperations, see Use Indexes to Sort Query Results.