Over a million developers have joined DZone.

Indexing in MongoDB

DZone's Guide to

Indexing in MongoDB

Analyze the main types of Indexes that MongoDB provides to increase performance for accessing data, including Default_id, Single Field, Compound, and more.

Free Resource

Slow SQL Server? These SentryOne resources share tips and tricks for not only troubleshooting SQL Server performance issues, but also preventing them before they hit your production environment.

Different Types of Indexing in MongoDB

MongoDB provides the following Indexes to increase performance for accessing data:

1. Default_id Index

2. Single Field Index

3. Compound Index

4. Multi-Key Index

5. Special Index (Geospatial, Text, TTL)

6. Hashed Indexes

Let’s discuss each one of the Indexes.

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

      "Desc":"Story about a doting father"+iCounter, 
                    "GrpName":"18+", "Desc":" Can be watched by 18+ years old"+iCounter 

The above collection will be used in this article to explain the concepts.

We can search for an item in the collection with and without creating the Index and use the explain command with executionstats parameter to understand the performance difference better.

Default_id Index

This is a unique Index created on the ID field of the collection. Please note that we cannot drop this Index, as it’s the default.

Try the below command to see the default Index.

                "v" : 2,
                "key" : {
                        "_id" : 1
                "name" : "_id_",
                "ns" : "test.Asset"

Single Field Index

Apart from the Default_id Index created by MongoDB, it allows us to create a user-defined Index.

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

Image title

Let’s create a single field Index using CLI:

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

Value 1 will enable ascending sorting on grade wherein Value—1 will enable descending sorts 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 the explain command now to see the difference:


Image title

Compound Index

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

Image title

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

Image title

Note: MongoDB imposes a limit of 31 fields in a compound Index.

Multi-Key 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.


Image title


Result of the explain command after multi-index:

Image title

Special Index: 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.

         Desc: "text"

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

The above command will list all the documents that contain "Doting" in the description.

Special Index: Geospatial Index

MongoDB allows us to create special Indexes for geospatial queries using geospatial Indexes.

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

The value can be 2d > legacy coordinate pairs (longitude, latitude)


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

Special Index: TTL

MongoDB supports time to live indexing, which can be used to remove certain documents after a certain time period. It is specifically useful on the log data and session information, as we don’t need this for a long time.

{ "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.

Note: 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.

Hashed Indexes

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

Hashed keys don't support range queries. In sharding, if you are looking to use hash-based partitioning, then we need to have hashed Indexes on the shard key.


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

Database monitoring tools letting you down? See how SentryOne empowers Enterprises to go faster.

nosql ,database ,mongodb ,indexing ,hashing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}