DEV Community

Cover image for Let's learn about Indexes in MongoDB
The Nerdy Dev
The Nerdy Dev

Posted on

Let's learn about Indexes in MongoDB

Hey everyone πŸ‘‹πŸ»,

In this article, let us learn about indexes in MongoDB.
So let us what an index in database is and what purpose it serves in context to working with a database.

Alt Text

Working with Indexes

Indexes is a typical database feature that can help us in retrieving the data efficiently. Indexes are features that can drastically increase the speed of your query if used in a correct manner. But if you used used incorrectly they can also slow down some of the operations

So let us see what we will cover in this article :

What are indexes ?
Different Types Of indexes
Using and Optimizing indexes

What are Indexes and why do we use them ?

An index can speed up our find, update or delete queries, basically all the queries where we are looking for some documents that match some criteria.

Let us see an example:
Let us say we have a books collection, we want to find all the books where the name of the author is Alex.

db.books.find({author : "Alex"})

Here in this query, since no index is there Mongodb will perform a
collection scan and look for each and every document in the collection. It will try to find a document within the books collection where the name of the author is Alex. For a very large collection of documents where there are thousands or even millions of documents, finding that one document which matches the filter criteria will definitely take some time.
It will first scan all the documents and then perform the filter
operation. But in order to perform your query in a time efficient manner, you can create an index.

The index is an ordered collection of values of the fields
for which you created the index and it is not for the documents
in the collection but rather for the fields for which you created
the index.

For example, here we can create an index for author index,

author index


Every item in index has a pointer to full document it belongs to.
This allows MongoDB to perform an index scan for the query to
fulfill. It sees for author that if such an index exists and it simply goes to the author index and can then quickly jump to the right value.
It can very quickly go through the index and find the matching
document because of the ordering and this pointer that I just mentioned. Mongodb finds the value for this query and then finds the related documents and returns them.

Creating such indexes can drastically speed up your queries.
Directly jump to the filtered documents.

Don't use too many indexes

An index does not come for free you will pay some performance
costs for insert because the extra index that needs to be maintained needs to be updated with every insert. If you add a new document you will have to add a new index. Since indexes do not come for free, you will have to find out which indexes makes sense and which do not.

Create an index

You can create indexes on embedded fields just like you could use normal fields. Let us see a simple example :

"a": 1
unique: true,
sparse: true,
expireAfterSeconds: 3600

If the options specification had been split into multiple documents like this: { unique: true }, { sparse: true, expireAfterSeconds: 3600 } the index creation operation would have failed.


If you have a query that returns 10 to 20% of the data of the dataset or just the patch of the data according to some filter criteria.In that case index will almost instantly speed up the process. If the documents are close to all the documents, indexes cannot do much in that case here prefer the collection scan rather than the index scan.

Indexing should quickly let you get to the narrowed down set of documents rather than the majority of that. This is the main idea behind indexing.

So this is it for this article.

PS - If you are looking to learn Web Development, I have curated a FREE course for you on my YouTube Channel, check the below article :

Looking to learn React.js with one Full Project, check this out :

πŸ‘‰πŸ» Follow me on Twitter :

πŸ‘‰πŸ» Check out my YouTube Channel :

Discussion (0)