Open In App

MongoDB – Multikey Indexes

Improve
Improve
Like Article
Like
Save
Share
Report

Indexes are special data structures that store some information related to the documents such that it becomes easy for MongoDB to find the right data file. They also store the value of a specific field or set of fields, ordered by the value of the field as specified in the index. MongoDB allows to index a field that holds an array value by creating an index key for each element in the array, such type of indexing is called Multikey indexes. It supports efficient queries against array fields. It can be constructed over arrays that hold both scalar values(like strings, numbers, etc) and nested documents.

How to Create Multikey Index?

In MongoDB, we can create Multikey indexes using the createIndex() method

Syntax:

db.Collection_name.createIndex({filed_name: 1/ -1})

Important Points:

  • If an indexed field is an array, then MongoDB will automatically create a multikey index for that field.
  • You are not allowed to specify a multikey index as the sherd key index.
  • In MongoDB, hashed indexes are not multikey index.
  • The multikey index cannot support the $expr operator.
  • In MongoDB, if a filter query specifies the exact match for an array as a whole, then MongoDB scans the multikey index to look for the first element of the quarry array. After scanning the multikey index to look for the first element of the quarry array, MongoDB retrieves those documents that contain the first element and then filter them for the document whose array matches the given query. Rather than scanning the multikey index to find the whole array.

Examples:

In the following example, we are working with:

Database: gfg

Collections: student

Document: Three documents contains the details of the students

  • Creating a multikey index:

Now we create multi-index with the help of createIndex() method on the field language:

db.student.createIndex({language:1})

After indexing, we will check the index using the getIndexes() method:

  • Indexing an array with the embedded document:

You are allowed to create a multikey index on an array field that contains nested documents/objects.

db.student.createIndex({"details.age":1, "details.branch":1})

Here, we create the multikey index on the “details.age” and “details.branch” fields.

After indexing, we will check the index using the getIndexes() method:

Limitations:

  • When you are creating a compound multikey index then each indexed document must contain at most one indexed field whose value is an array.
  • You are not allowed to create a compound multikey index if more than one to be indexed field of a document is an array.
  • When the compound multikey index already exists, then you are not allowed to insert a document that will break the restrictions.
  • When a field is an array of documents, you are allowed to index an embedded document to create a compound index but at most one indexed field can be an array.

Last Updated : 17 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads