In simple words “indexes are used to speed up query performance”. Let’s say you have a query which takes around 120ms to execute without indexes i can assure you that with proper indexes you can reduce that time by at least 10 times. Let me show you with an example.

First of all we need to create a big collection which contains few million records. It’s easy to create such a collection from shell because it supports javascript.

The above command will take some time to execute in your shell because we are entering lot of records in a collection. If you want to adjust values for number of records feel free to do so. Once the data has been entered into the collection we can query it normally.

Now we are sure that we have 1000000 records in our collection and that’s big enough for us to test indexes.

Running a query on such a big collection brings some questions to my mind :

1. How much time was consumed in executing this query ?
2. How many records were scanned ?
3. Did this query use any index ?

MongoDB already provides with inbuilt functions which can answers all above questions in one go. The answer is explain(). Let’s see an example :

Now our questions have been answered and we know that query took 40 ms to execute. How about we create an index now and use that ? So this is how we create index in MongoDB

But we need to verify if any index was created or not. Here we go

Now let’s run the same query again to find all distances above 555500.

You must have noticed that following changes happened with the use of index.

Time without index : 40 ms
Records scanned without index : 1000000

Time with index : 0 ms
Records scanned with index : 0

You can find other post in MongoDB series at this link