MongoDB

How to create an index in MongoDB

MongoDB being a NoSQL database, provides extensive support for querying saved documents inside a collection. In MongoDB, the querying process can be made fast by creating an index of a field in a collection of MongoDB. The index then holds the data for that specific field and provides assistance to retrieve those documents fast.

The indexing of fields really helps when working on big databases. For instance, the query does not need to look for the entire collection for a document; it just looks for indexes and shows the result. Our today’s article is a part of the MongoDB series and we will demonstrate the way to create an index in MongoDB.

How the indexing phenomenon works in MongoDB

The indexes in MongoDB are created on fields and the following syntax must be adopted to create an index in MongoDB:

Syntax

db.collection-name.createIndex({<field-name>: <order>})

The <field-name> refers to the field of a collection for which you want to create an index, and the <order> points towards an ascending or descending order of documents while creating an index. For ascending order, “1” is used, and “-1” is used for descending order.

How to create an index in MongoDB

The indexing in MongoDB is primarily exercised to pace up the processing in MongoDB. The MongoDB collection we will target here is named “staff,” and the documents contained by it are:

> db.staff.find().pretty()

In the upcoming sections, the above-shown data will be used to execute commands.

The most used index types in MongoDB are:

Single Index: Used to refer to only one field of a document in a collection

Compound Index: That refers to multiple fields

Multikey Index: Specifically used to create an index for array data fields

How to create a Single field Index

The single field index means that an index will be created on a single field of a document. The below-stated command will create an index on the “name” field of the “staff” collection:

> db.staff.createIndex({name: 1})

The output shows that there was only one index present before this command, and after the command’s execution, the total number of indexes became 2.

Additionally, you can set name to any index that you create. For example, the below mentioned command will set a name “index on std name“:

> db.staff.createIndex({name: -1}, {name: "index on student name"})

Note: The “1” or “-1” in the above commands show the ascending or descending order of the index.

How to create Compound Index

You can also create an index by referring to two fields. The command written below will create an index on “name” and “desig” of the “staff” collection:

> db.staff.createIndex({name: -1, desig: 1})

Note: With the help of the compound index used here, MongoDB will first look for the “name” field in ascending, and after that, it sorts the document according to the “desig” [designation] field in descending order.

How to create Multikey Index

As stated earlier, the Multikey index is used to create an index for an array field. MongoDB creates a multikey index when an array field is passed into createIndex(). It is noticed that you do not need to mention that it is an array field; instead, the createIndex() function automatically recognizes the array.

In our case, the “staff” collection contains an array field named “cat” that represents the Operating System’s category with which a specific staff member deals.

The command provided here will create index for each element of an array field:

> db.staff.createIndex({cat: 1})

How to find indexes in MongoDB

After the creation of indexes, you can find all available indexes on your system by issuing the below-mentioned command:

> db.staff.getIndexes()

You will observe that all the indexes created in the above section are shown here.

How to drop an index in MongoDB

To drop any specific index, you can follow any one of the syntaxes provided below:

db.collection-name.dropIndex(<index-name>)

The <index-name> shows the name that you assigned to a specific index:

Or:

db.collection-name.dropIndex({<field-name>: <order>})

The <field-name> and <order> represent the exact field name and the index order in which it was created.

Example: For instance, the below-mentioned dropIndex() method’s command will remove the index that was created on the “name” field and order was “1.

Note: All the indexes that fall under the criteria are dropped after executing the command.

> db.staff.dropIndex({name:1})

Or you can use the following command to drop all indexes on your collection.

> db.staff.dropIndexes()

Note: Only the default index of the system is left behind, and the rest all are deleted.

Conclusion

MongoDB provides a fast and effective querying system backed up by MongoDB Query Language(MQL). While querying documents, indexes play a vital role in executing the query in significantly less time. In this article, we have explained the “index” concept in MongoDB. After reading this guide, you will be able to create an index on a single field, compound fields, and array fields. Moreover, you can also find the available indexes, and the way to drop any index is also described in this post.

About the author

Adnan Shabbir