MongoDb: Indexing

In this tutorial we will talk about indexing. In a textbook you all remember about the index page that contains the metadata, which give details about the chapter number, chapter name and the corresponding page number. Suppose you want to go to chapter number 4, so you just open the index, search for the chapter number 4 and then go to the corresponding page number.

Indexing in mongodb is similar like the above. Insense that mongodb not have to scan each and every document when you have the index present.


Retrieve the document from the database

Note: We are using testDB database, and from the previous tutorial we are using collection as testCollection

1) Open a new command Prompt

2) Enter “mongod“, and press ENTER (it will start your mongodb server. Don’t close it)

3) Open Robo3T.

4) Right click on Connection name.

5) Open shell.

6)Write “use testDB“, and press CTRL+ENTER. It will switch to testDB database.

7) Write “db.testCollection.find({})” , and press CTRL+ENTER.

You find

First element as Name: SHIP, Value: F, Price: 50
Second element as Name: BIKE, Value: B, Price: 40
Third element as Name: Truck, Value: E, Price: 35
Fourth element as Name: Bicycle, Value: D, Price: 25
Fifth element as Name: Car, Value: A, Price: 20
Sixth element as Name: MOTORCYCLE, Value: C, Price: 10


Indexing in Mongodb

Mongodb command is : “db.collectionName.ensureIndex({“indexFieldName”:1})

indexFieldName denotes the field name through which you want to use indexing.

Example: Index the documents in testCollection on the basis of Name.

MongoDB command is: “db.testCollection.ensureIndex({”Name”:1})

Write MongoDB command in shell and press CTRL+ENTER.

Output will be OK.


Getting the Indexes in MongoDB

 Mongodb command is : “db.collectionName.getIndexes()

Example: Get all the indexes from the testCollection.

MongoDB command is: “db.testCollection.getIndexes()

Write MongoDB command in shell and press CTRL+ENTER.

It gives two outputs as:

One index is been made by default by the mongodb, and the second index is being the index that you have made above on the basis of Name.


Drop Indexes in MongoDB

Mongodb command is : “db.collectionName.dropIndex({“indexFieldName”:1})

indexFieldName denotes the field name through which you have use indexing.

Example: Drop the Index of the documents in testCollection on the basis of Name.

MongoDB command is: “db.testCollection.dropIndex({”Name”:1})

Write MongoDB command in shell and press CTRL+ENTER.

Output will be OK.

When you again write “db.testCollection.getIndexes()” in the shell and pres CTRL+ENTER, you will find that index of the Name is been dropped.


Advantages of Indexing

When you have million of documents, querying the data. It going to take huge amount of time. Using index will lower the querying time.

Note: Never use indexing at each field in a document. By doing so, it means then you just be recreating the document itself. Always index those field which has the unique values among all the documents.For example, Email-id, Username etc.

If you have any queries regarding indexing documents in MongoDB, drop a comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *