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.
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.
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
db.collectionName.createIndex({"field1": 1});
db.collectionName.createIndex({"field1": 1, "field2": 1});
db.collectionName.createIndex({"field1.subfield": 1});
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");
Following are three keys whose value you would want to watch out for in the output of above command execution.
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…