The Wayback Machine - https://web.archive.org/web/20250306124833/https://www.geeksforgeeks.org/mongodb-multikey-indexes/
Open In App

MongoDB Multikey Indexes

Last Updated : 25 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

MongoDB multikey indexes are essential for optimizing queries involving fields that contain array values. These indexes are automatically created by MongoDB when an array field is indexed and allowing for efficient querying and sorting of data within arrays. In this article, We will learn about the MongoDB Multikey Indexes in detail.

MongoDB Multikey Indexes

  • MongoDB multikey indexes are used to collect and sort data from fields containing array values.
  • MongoDB automatically creates a multikey index when a field that contains an array value is indexed.
  • Multikey indexes help make searches on arrays faster.
  • It can be constructed over arrays that hold both scalar values(like strings, numbers, etc) and nested documents

How to Create a Multikey Index?

To create a Multikey Index in MongoDB use the createIndex() method.

Syntax:

The syntax to create a Multikey Index is given below:

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

Examples of MongoDB Multikey Indexes

In the following example, we are working with:

Database: gfg

Collections: student

Document: Three documents contains the details of the students

Output:

demo database and collection

Example 1: Index Basic Arrays

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

Query:

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

Output:

createindex method

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

getindex method

Example 2: Index Arrays with Embedded Documents

We 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})

Output:

create index with embedded document

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

get index method

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

Limitations of MongoDB Multikey Indexes

  • 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.

Key TakeAways About MongoDB Multikey Indexes

  • Multikey indexes in MongoDB are used to collect and sort data from fields containing array values, improving query performance for array fields.
  • You can’t use a multikey index as the shard 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.

Conclusion

In summary, multikey indexes in MongoDB significantly enhance query performance for fields containing arrays. They can be created using the createIndex() method and support efficient queries on both simple arrays and arrays with embedded documents. However, it is important to be aware of the limitations and constraints, such as restrictions on compound multikey indexes and incompatibility with certain operators like $expr.

FAQs on Multikey Indexes in MongoDB

What are MongoDB multikey indexes?

MongoDB multikey indexes are used to index fields containing array values. They allow for efficient querying and sorting of data within arrays.

How are multikey indexes created in MongoDB?

Multikey indexes can be created using the createIndex() method in MongoDB. This method is used to specify the field containing array values and the direction of the index (ascending or descending).

What are the limitations of MongoDB multikey indexes?

One limitation is that each indexed document can have at most one indexed field that is an array. Additionally, compound multikey indexes cannot be created if more than one indexed field of a document is an array.

Can multikey indexes be used as shard key indexes in MongoDB?

No, multikey indexes cannot be used as shard key indexes in MongoDB


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg