![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
MongoDB - interpret information on the query plan for the db.collection.find() method
For information on the query plan, use explain() in MongoDB. Let us create a collection with documents −
> db.demo637.ensureIndex({ClientName:1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo637.insert({ClientName:"John"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Bob"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Johnson"}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo637.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c26916c954c74be91e6db"), "ClientName" : "John" } { "_id" : ObjectId("5e9c26936c954c74be91e6dc"), "ClientName" : "Bob" } { "_id" : ObjectId("5e9c269d6c954c74be91e6dd"), "ClientName" : "Johnson" }
Case 1 −
Here is the query for regexp / / −
> db.demo637.find({ClientName:/john/}).explain();
This will produce the following output −
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo637", "indexFilterSet" : false, "parsedQuery" : { "ClientName" : { "$regex" : "john" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "filter" : { "ClientName" : { "$regex" : "john" } }, "keyPattern" : { "ClientName" : 1 }, "indexName" : "ClientName_1", "isMultiKey" : false, "multiKeyPaths" : { "ClientName" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "ClientName" : [ "[\"\", {})", "[/john/, /john/]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }
Case 2 −
Following is the query for regexp /^ / −
> db.demo637.find({ClientName:/^john/}).explain();
This will produce the following output −
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo637", "indexFilterSet" : false, "parsedQuery" : { "ClientName" : { "$regex" : "^john" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "ClientName" : 1 }, "indexName" : "ClientName_1", "isMultiKey" : false, "multiKeyPaths" : { "ClientName" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "ClientName" : [ "[\"john\", \"joho\")", "[/^john/, /^john/]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }
Advertisements