MongoDB – Part 5 – Indexing

By | April 18, 2018

Different Types of indexing in MongoDB

MongoDB provides the following indexes to increase the performance for accessing the data

  1. Default _id Index
  2. Single Field Index
  3. Compound Index
  4. Multikey Index
  5. Special Index [ Geospatial, Text, TTL]
  6. Hashed Indexes

Let’s discuss about at each one of the Index:

As a first step let’s execute the following script for creating “Assets” collection.



"Desc":"Story about a doting father"+iCounter, 



"GrpName":"18+", "Desc":" Can be watched by 18+ years old"+iCounter 

We can search for a query before and after creating the index and the executionstats to understand it better.

  1. Default _id Index

It is an unique index created on the ID field of the collection. Please note that we cannot drop this index as it’s default. 

Try the below command to see the default index.

> db.Asset.getIndexes()



                “v” : 2,

                “key” : {

                        “_id” : 1


                “name” : “_id_”,

                “ns” : “test.Asset”




2.   Single Field Index

Apart from the default _ID index created by MongoDB, it allows us to create user defined index.

Let’s run the explain command with executionstats before creating the index just to see the difference it is bringing in.

Let’s create a Single Field Index using CLI

> db.Asset.createIndex( { Rank : 1 } )

Value 1 will enable ascending sort on grade where in Value  – 1 will enable descending sort on grade by traversing the index in reverse order.

Note: We have ensureIndex() which does the same job. This is deprecated from V3.0 as mentioned in the mongo documents.

Run > db.Asset.explain(true).find({Rank:1})

You can compare the totaldocsexamined before and after creation of index to see the benefits.

3.   Compound Index

MongoDB allow us to create a single structure to refer multiple fields.

If you repeat the getIndexes() you can find the following:


 MongoDB imposes a limit of 31 fields in compound index.

 4.   Multikey Index

 MongoDB allows multi key indexes to support efficient queries against arrays. It creates an Index key for each element in the array.

This applies to scalar values as well as embedded documents.


> db.Asset.createIndex({“Language”:1}

Result of Explain command after Multi Index

5. Special Index [ Geospatial, Text]

Text Index:

MongoDB allows us to create a text index on the field or fields whose value is a string or an array of string elements. When creating a text index on multiple fields, you can specify the individual fields or you can use wildcard specifier ($**) which creates text index on all string fields in that document.

> db.Asset.createIndex(


Desc: "text"



> db.Asset.find( { $text: { $search: "Doting" } } )

The above command will list all the documents that contains “Doting” text in the description.

Geospatial Index:

MongoDB allows us create index specially for geospatial queries using Geospatial indexes.

db.places.createIndex( { hotellocation: “2d” } )

Value can be of

2d -> legacy coordinate pairs ( longitude,latitude )


A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity.


MongoDB supports Time To Live indexing which can used to remove certain documents after certain time period. It is specifically useful on the log datas, session informations as we don’t need this for so long time.

> db.Events.insert({ “EventDesc”:” user has clicked to detail page with trackcode 103″, “EventDateTime”: new Date() } )

> db.Events.createIndex( { “EventDateTime”: 1 }, { expireAfterSeconds: 30 } )

You can notice the record disappearing in 30 seconds post insertion.


The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time a document expires and the time that MongoDB removes the document from the database.

The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.

6.Hashed Indexes

MongoDB allows us to create hashed index to reduce the size of the indexes. It is because only the hash is stored in the index instead of the entire key.

Hashed Key doesnot support range queries. In sharding if you are looking to use hash based partitioning then we need to have hashed indexes on the shardkey.

> db.collection.ensureIndex({attribName:’hashed’});

This was a long post, but I hope it was helpful!

Leave a Reply

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