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.

for (val=0; val < 1000000; val++) {
	db.destination.save({distance:val});
}

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.

db.destination.count();
1000000

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

db.destinations.find({distance:555500});
{ "_id" : ObjectId("537e3ccdf252b602fb92a9dc"), "distance" : 555500 }

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 :

db.destinations.find({distance:555500}).explain();
{
	"cursor" : "BasicCursor",
	"isMultiKey" : false,
	"n" : 99899,
	"nscannedObjects" : 1000000,
	"nscanned" : 1000000,
	"nscannedObjectsAllPlans" : 1000000,
	"nscannedAllPlans" : 1000000,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 781,
	"nChunkSkips" : 0,
	"millis" : 40,
	"server" : "gaurav-Aspire-E1-572:27017",
	"filterSet" : false
}

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

db.destination.ensureIndex({distance:1});	
// It will create an index on distance field 
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

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

db.destination.getIndexes();
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "tutorial.dd"
	},
	{
		"v" : 1,
		"key" : {
			"distance" : 1
		},
		"name" : "distance_1",
		"ns" : "tutorial.destination"
	}
]

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

db.destinations.find({distance:555500}).explain();
{
	"cursor" : "BtreeCursor distance_1",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"distance" : [
			[
				555500,
				555500
			]
		]
	},
	"server" : "gaurav-Aspire-E1-572:27017",
	"filterSet" : false
}

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