MongoDB Indexing

MongoDB Indexing

An index in MongoDB is a special data structure that holds the data of few fields of documents on which the index is created. Indexes improve the speed of search operations in database because instead of searching the
whole document, the search is performed on the indexes that holds only few fields. On the other hand, having too many indexes can hamper the performance of insert, update and delete operations because of the additional write and additional
data space used by indexes.

How to create index in MongoDB

Syntax:

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

The value 1 is for ascending order and -1 is for descending order.

For example, I have a collection employee data. The documents inside this collection have following fields:

_id, dept_id, and name

I want to create the index on ‘name’ field in ascending order:

db.department.createIndex({name: 1})

Output :

    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1, //Number of indexes before the command is executed (_id)
    "numIndexesAfter" : 2, //Number of Indexes after command execution (_id and the one we have created)
    "ok" : 1 //Command is Successful
                

We have created the index on ‘name’ which means when someone searches the document based on the ‘name’, the search will be faster because the index will be used for this search. So this is important to create the index on the field that
will be frequently searched in a collection.

MongoDB – Finding the indexes in a collection

We can use getIndexes() method to find all the indexes created on a collection. The syntax for this method is:

db.collection_name.getIndexes()

So to get the indexes of ‘department’ data collection, the command would be:


    db.department.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "employeeDB.department"
            },
            {
                    "v" : 2,
                    "key" : {
                            "name" : 1
                    },
                    "name" : "name_1",
                    "ns" : "employeeDB.department"
            }
    ]
                

The output shows that we have two indexes in this collection. The default index created on ‘_id’ and the index that we have created on ‘name’ field.

MongoDB – Drop indexes in a collection

You can either drop a particular index or all the indexes.

Dropping a specific index:

For this purpose the dropIndex() method is used.

db.collection_name.dropIndex({index_name: 1})

Lets drop the index that we have created on ‘name’ field in the collection department data. The command for this:

db.department.dropIndex({name:1})

Output :

    db.department.dropIndex({name:1})
    { "nIndexesWas" : 2, "ok" : 1 }
                

Dropping all the indexes:

To drop all the indexes of a collection, we use dropIndexes() method.

Syntax of dropIndexs() method:

db.collection_name.dropIndexes()

For example we want to drop all the indexes of department data collection.

db.department.dropIndexes()

Leave a Reply

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