Open In App

MongoDB Multikey Indexes

Last Updated : 18 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 allowing for efficient querying and sorting of data within arrays. In this article, We will learn about the MongoDB Multikey Indexes in detail.

What are MongoDB Multikey Indexes?

MongoDB Multikey Indexes are specialized indexes used to optimize queries on fields containing arrays. MongoDB automatically creates a multikey index when an array field is indexed. These indexes allow for efficient querying of individual elements within arrays, reducing the time it takes to fetch results when querying large datasets with nested or array data.

  • Multikey indexes are crucial for improving the performance of queries that involve fields containing arrays of scalar values (like strings or numbers) and nested documents (objects or sub-documents).
  • They allow MongoDB to index each element of the array so queries on array data can be executed efficiently.

How to Create a Multikey Index?

When we create an index on an array field, MongoDB treats each element of the array as a separate indexed value. This enables efficient searching and sorting based on the elements in the array.

Example: If we have an array field containing different types of programming languages, a multikey index will allow you to efficiently query whether a particular language exists in the array for each document.

Syntax:

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

In this case, MongoDB creates a multikey index on the languages array. The index will store each individual element in the languages array for quick lookup.

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

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

While MongoDB multikey indexes offer significant performance improvements, they come with some limitations and constraints:

  • Compound Multikey Indexes: When creating a compound multikey index (an index on multiple fields), MongoDB imposes a restriction: each document can only contain one indexed field that is an array. Example: If one indexed field is an array, the other indexed fields must not be arrays.
  • Shard Key Restriction: Multikey indexes cannot be used as shard key indexes. When sharding a collection, it’s crucial to understand that MongoDB will not allow us to use an array field as a shard key.
  • Limitations with $expr Operator: The $expr operator, which allows for evaluating expressions within queries, is not supported by multikey indexes.
  • Insertion Restrictions: Once a compound multikey index is created, you cannot insert a document that breaks the indexing restrictions (e.g., inserting an array in multiple indexed fields).

Practical Example of MongoDB Multikey Indexes

Consider a scenario where you have a collection of students. Each student has multiple subjects they are enrolled in, and some students may have additional details like age and branch.

Schema

{ "_id": 1, "name": "Alice", "subjects": ["Math", "Science"], "details": { "age": 20, "branch": "CS" } }
{ "_id": 2, "name": "Bob", "subjects": ["Math", "English"], "details": { "age": 22, "branch": "IT" } }

Index Creation

db.students.createIndex({ subjects: 1 })  // Create a multikey index on the 'subjects' array
db.students.createIndex({ "details.age": 1, "details.branch": 1 }) // Compound index on embedded fields

With these indexes in place, MongoDB can now efficiently perform queries on both the subjects array and the details embedded fields.

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



Next Article
Article Tags :

Similar Reads