- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Does MongoDB track how many times each index is used in a query?
Yes, you can track how many times each index is used in a query using MongoDB $indexStats. Following is the query to create an index in MongoDB −
> db.demo508.createIndex({"FirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Let us create a collection with documents −
> db.demo508.insertOne({"FirstName":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e883818987b6e0e9d18f578") } > db.demo508.insertOne({"FirstName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88381b987b6e0e9d18f579") } > db.demo508.insertOne({"FirstName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88381f987b6e0e9d18f57a") }
Display all documents from a collection with the help of find() method −
> db.demo508.find();
This will produce the following output −
{ "_id" : ObjectId("5e883818987b6e0e9d18f578"), "FirstName" : "John" } { "_id" : ObjectId("5e88381b987b6e0e9d18f579"), "FirstName" : "Chris" } { "_id" : ObjectId("5e88381f987b6e0e9d18f57a"), "FirstName" : "David" }
Following is the query to track how many times each index is used in a query −
> db.demo508.aggregate([{ $indexStats: { } }]).pretty();
This will produce the following output −
{ "name" : "_id_", "key" : { "_id" : 1 }, "host" : "DESKTOP-QN2RB3H:27017", "accesses" : { "ops" : NumberLong(0), "since" : ISODate("2020-04-04T07:32:27.394Z") } } { "name" : "FirstName_1", "key" : { "FirstName" : 1 }, "host" : "DESKTOP-QN2RB3H:27017", "accesses" : { "ops" : NumberLong(0), "since" : ISODate("2020-04-04T07:32:27.527Z") } }
Advertisements