MongoDB Indexes are used to provide efficient execution of queries that are fired from the Client. Without indexes, MongoDB has to perform the complete scan of every Document present in the Collections and get the result that is a time and resources consuming task. Using Index the MongoDB can select the appropriate Collection and avoid the Complete scanning of Collections.

In MongoDB, Indexes are created at the Collection level and it is the data structures that store the small data set of collection to perform the easy search operation.

In the following diagram, there is a Collection name called "users" on which the query is executing. The query is selecting the Documents where the "score" is less than 30, and sorting it in a descending order using the index. So after processing the query, the result of "score" would be 25,5,18.


Mongodb indexing


Let us see the MongoDB Index methods in the following section.

db.collection_name.createIndex()

MongoDB createIndex() method is used to create an index on the Collection of Documents. MongoDB creates a default index on the _id field of Collection and provides the facility to create additional Indexes using the createIndex() method to support the queries and operation.

Syntax:

The following is the syntax of the createIndex() method. The 1 represents the ascending order and the -1 represents the descending order.

db.collection_name.createIndex({keyname:1 or -1})

Let us see an example to create an Index on the "order_date" Document of the "orders_detail" Collection that we have already created in the aggregation section. The "orders_detail" Collection is present under "mongodb_test" database.

Example:

In the following example, the Index is created in an ascending order using 1. To create index in decending order we can use -1.

show dbs
> use mongodb_test
> db.orders_detail.createIndex( { "order_date": 1 } )

Output:

Mongodb create index

We can create an Index on multiple Documents as well. For example, we have created indexes on "order_date" in ascending order and on "Date" in descending order.

Example:

db.orders_detail.createIndex( { "order_date": 1, "Date": -1 } )

Output:

Mongodb create multiple index


db.collection_name.getIndexes()

The MongoDB getIndexes() method is used to show the description of all Indexes of a Collection.

Syntax:

The following is the syntax of getIndexes() method.

db.collectionname.getIndexes()

In the below example we can the Index detail of the Collection "orders_detail".

Example:

db.orders_detail.getIndexes()

Output:

Mongodb getindexes


db.collection_name.dropIndex()

MongoDB dropIndex() method is used to drop a specific index of a Document.

Syntax:

The following is the syntax of dropIndex() method.

db.collectionname.dropIndex({keyname: key_value})

Let us see the below example to drop an index that is created on Document "order_date" of "orders_detail" Collection.

Example:

db.orders_detail.dropIndex({"order_date": 1})

Output:

From the below output, we can see that the index has been dropped.

Mongodb drop index


db.collection_name.dropIndexes()

MongoDB dropIndexes() method is used to drop multiple indexes.

Syntax:

The following is the syntax of dropIndexes() method.

db.collectionname.dropIndexes({keyname1: key_value1, keyname2: key_value2})

We will see an example to drop multiple indexes "order_date" =1, "Date" =-1 that is created above for "orders_detail" Collection.

Example:

db.orders_detail.dropIndexes({"order_date": 1, "Date": -1})

Output:

Mongodb drop indexes