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

Ajitesh Kumar

Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, C/C++, mobile programming languages etc, and, computing fundamentals related with cloud-native technologies, application security, cloud computing platforms, mobile apps, big data etc.

He has also authored the book, Building Web Apps with Spring 5 and Angular.
Ajitesh Kumar

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.