Find Out if a Query uses an Index in MongoDB

In MongoDB, you can use the cursor.explain() method or the db.collection.explain() method to determine whether or not a query uses an index.

These methods enable you to view the query plan for the query, which includes whether or not it uses an index.

Example

Suppose we have a collection called pets, and it contains the following documents:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 }
{ "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 }
{ "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }

And suppose we create the following index on its name field:

db.pets.createIndex( { "name" : 1 } )

Now when we run the following query, it should use that index:

db.pets.find( { "name" : "Scratch" } )

Result:

{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }

But we can’t tell just by looking at the results whether it used the index or not.

This is where the explain() method comes in. We can append explain() to the end of our query to get the query plan. That will tell us whether or not it used an index.

db.pets.find( { "name" : "Scratch" } ).explain()

Result:

{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "PetHouse.pets",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"name" : {
				"$eq" : "Scratch"
			}
		},
		"queryHash" : "01AEE5EC",
		"planCacheKey" : "4C5AEA2C",
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"name" : 1
				},
				"indexName" : "name_1",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"name" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"name" : [
						"[\"Scratch\", \"Scratch\"]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"ok" : 1
}

We can see by the part that reads IXSCAN that the query uses an index scan to produce its results.

In contrast, if we do the same for a query that isn’t included in our index, we’ll see that it uses a collection scan (COLLSCAN):

db.pets.find( { "type" : "Dog" } ).explain()

Result:

{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "PetHouse.pets",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"type" : {
				"$eq" : "Dog"
			}
		},
		"queryHash" : "2A1623C7",
		"planCacheKey" : "2A1623C7",
		"winningPlan" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"type" : {
					"$eq" : "Dog"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"ok" : 1
}

The db.collection.explain() Method

The db.collection.explain() method is similar to cursor.explain(), except that with db.collection.explain(), you can chain additional query modifiers to the query (after the find() method).

For our purposes, we can do the following:

db.pets.explain().find( { "name": "Scratch" } )

Result:

{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "PetHouse.pets",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"name" : {
				"$eq" : "Scratch"
			}
		},
		"queryHash" : "01AEE5EC",
		"planCacheKey" : "4C5AEA2C",
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"name" : 1
				},
				"indexName" : "name_1",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"name" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"name" : [
						"[\"Scratch\", \"Scratch\"]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"ok" : 1
}

You can run the following command to retrieve a list of query modifiers available for this method:

db.collection.explain().find().help()