Introduction
A Database is a collection of the necessary data and information. MongoDB is a NoSQL kind of database management system. It can store and handle large volumes of unstructured or semi-structured data with no fixed schema. In it, indexing is required to fetch data quickly. But, there are different kinds of indexing in dbms.

In this article, we will study Multikey Indexes.
What are Multikey Indexes?
First of all, let us understand indexing in dbms. Indexes are unique data structures that store the mapping of the data addresses from their table to the original database. It helps fetch the data quickly instead of scanning the central database thoroughly. We must browse the index table and find our corresponding location in the database.
Database management systems like MongoDB allow indexing a database field with various elements. It achieves this by creating an index key for all the elements in the array. It is called multikey indexing. It allows indexing over both scalar values and nested documents.
Some points to note when dealing with multikey indexes are as follows:
-
If the indexed field turns out to be an array, MongoDB automatically creates a multikey index.
-
Hashed indexes are not multikey indexes in MongoDB. Also, the multikey index does not support the $expr operator.
- When scanning a multikey index field, MongoDB checks for the first matching value, retrieves documents matching it, then reaches for other values in those retrieved documents rather than on all the records.
Creating a Multikey Index
Now, let us see how to create a multikey index. First, we must create some entries in our database named students. We create two different entries in it using the following commands:
Then, using the following command, we can see the entries:
OUTPUT


Next, we create an index on the field hobbies using the following command:
OUTPUT

Next, we will check the index using the following command:
OUTPUT

Here, we can see that there are multikey indexes for hobbies.
Indexing With Embedded Document
Now, we will create a multikey index on an array field that contains a nested document or objects using the following command:
OUTPUT

We create it on the languages field that has different objects as name1 and name2.
Now, let us check the indexes with the following command:
OUTPUT




