Multikey Indexes
To index a field that holds an array value, MongoDB creates an index key for each unique 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. If an array contains multiple instances of the same value, the index only includes one entry for the values.
[1] | A scalar value refers to value that is neither an embedded document nor an array. |
Compatibility
You can use multikey indexes for deployments hosted in MongoDB Atlas.
To learn more about managing indexes for deployments hosted in MongoDB Atlas, see Create, View, Drop, and Hide Indexes.
Create Multikey Index
To create a multikey index, use the
db.collection.createIndex()
method:
db.coll.createIndex( { <field>: < 1 or -1 > } )
MongoDB automatically creates a multikey index if any indexed field is an array; you do not need to explicitly specify the multikey type.
Note
For the WiredTiger and In-Memory storage engines only,
For multikey indexes, 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:
{ _id: 1, a: [ 1, 2 ], b: [ 1, 2 ], category: "AB - both arrays" } 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:
{ _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 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:
{ _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 }
. 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 4.4, when you sort on an array indexed with a multikey index, the query plan includes a blocking sort stage, unless:
The index boundaries for all sort fields are
[MinKey, MaxKey]
, andNo boundaries for any multikey-indexed field have the same path prefix as the sort pattern.
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.
Hashed Indexes
Hashed indexes cannot be multikey.
Covered Queries
Multikey indexes cannot cover queries over array field(s).
Multikey indexes can cover queries over non-array fields if the index tracks which field or fields cause the index to be multikey.
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:
{ _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 is
the array [ 5, 9 ]
:
db.inventory.find( { ratings: [ 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 ]
.
$expr
$expr
does not support multikey indexes.
Examples
Index Basic Arrays
Create a survey
collection with the following document:
db.survey.insertOne( { _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 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:
{ _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 that
include both indexed fields as well as predicates that include only the
index 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, 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:
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 sort operations, see Use Indexes to Sort Query Results.