MongoDB – Evaluate Query Performance using Indexes

This blog represents commands used to manage MongoDB indexes on a particular collection and tips on how to evaluate query performance with or without indexes. Please feel free to suggest. Sorry for typos.

Why create indexes?

Indexes can significantly improve the read query performance for MongoDB collections. In absence of indexes, when searching for documents based on filter criteria, MongoDB performs collection scan, i.e., scan every document and returns the documents matching the filter criteria. This is not very efficient way of searching the document. In case of one or more fields which are frequently used for filtering out the document, it is recommended to create indexes on those fields. MongoDB, thus, limits the number of document which is scanned in presence of indexes. This results in faster queries’ execution time owing to less number of documents being scanned.

It should be noted that MongoDB creates a unique index on the _id field during the creation of a collection.

Different kind of indexes?

Following are different kind of indexes which can be created on one or more fields of a MongoDB collection. Details can be found on MongoDB Indexes

  • Single field: Index created on a single field
    db.collectionName.createIndex({"field1": 1});
  • Compound index: Index created on multiple fields.
    db.collectionName.createIndex({"field1": 1, "field2": 1});
  • Multikey index: Index created on sub-documents
    db.collectionName.createIndex({"field1.subfield": 1});
  • Text index: Index created on string
  • Hashed index: Index created for supporting hash-based sharding

Commands to manage indexes

  • Create Index: db.collectionName.createIndex({“fieldName”: 1});
  • Remove Index: db.collectionName.dropIndex({“fieldName”:1});
  • Get Index Information: db.collectionName.getIndexes();

Command to examine query performance based on indexes inclusion/exclusion

Create (drop) indexes on one or more fields in a collection and execute the command given below to evaluate query performance with or without indexes.

db.collectionName.find({"fieldName": "some value"}).explain("executionStats");

Evaluate query performance metrics

Following are three keys whose value you would want to watch out for in the output of above command execution.

  • totalKeysExamined: Total number of index entries scanned
  • totalDocsExamined: Total number of documents scanned to find the results.
  • executionTimeMillis: Time required to excute the query
Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog,
Posted in MongoDB, NoSQL. Tagged with , .

Leave a Reply

Your email address will not be published. Required fields are marked *