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.
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…
In this blog, I aim to provide a comprehensive list of valuable resources for learning…
Have you ever wondered how systems determine whether to grant or deny access, and how…
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…