Habilelabs-Logo
Blog

Query Optimizer and Indexing in MongoDB

March 31st, 2017 . 3 minutes read
Blog featured image

What is indexing in MongoDB?

Basically, index is a part of data structure that helps to find or search data from the collection in an efficient manner. But MongoDB indexing eases this process and find the data by matching queries without performing whole collection scan.

If a collection is well indexed, then MongoDB uses the index to fetch data quickly because it uses an index and limits the amount of data to be examined.

MongoDB Indexing Types

Indexing in MongoDB has many types that help to execute data in an efficient way and optimize your query as well.
1. Single field index
2. Compound index
3. Multikey index
4. Geo index
5. Covered index
6. Hashed index

How to Create, Delete and get Index?

Simply run this command in your Mongo shell to create indexes-

• Collection means your collection name
• Type means you filed name that you want to index
• 1 for ascending order and -1 for descending order

How to Delete indexes –

How to Get All indexes –

Index Properties

Indexing has many properties like-

• Unique – It makes that field Unique that helps MongoDB to reject duplicate field of the collection. Just pass this during index creation.

• Sparse – A very useful property of index which ensures that the index takes only the data that have indexed field. Just pass this during index creation-

• Partial – It is similar like sparse but in partial we just make a filter condition, so if the data will match this filter index condition then it will take if not then simply index ignore this indexed field.

Key Point To Remember-

Mongo DB uses only one index at a time. If you have more than one field in your query then you will have to use compound index instead of the single index. Compound index matters with ordering so the order of query filed same as compound index field order, then it works perfectly.

Also, MongoDB optimizes best index that is suitable for the query so you have to not worry about this. It will take the best index that matches the query.

For example, if this is your query- 

Then both indexes not were using for finding data-

So only compound index will use for finding the data fast-

• Indexing was fast the data searching but it will slow the Insert/Update/Delete so indexing fast the writing process and slow the Read process

• If you using sorting in your query to sort data then it affects to your query and also slows the query process so it takes more time.
Just use the sorting filed in your index at last then it will optimize your query very effective like this-

• Use explain in your query to ensure that indexed is applied properly in winning plan-

I hope this article will help you understand the fast process of MongoDB and how it optimizes the search query with Index.

If you have any query related to Indexing in MongoDB then ask in a comment.

Hope you like this post, so don’t forget to share with friends on Facebook.

Author: balgopal
Share: