
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display MongoDB Explain Query Plan
For information on query plan, use explain() in MongoDB. Let us create a collection with documents −
> db.demo202.insertOne({"StudentFirstName":"Chris","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bd103d395bdc21346e8") } > db.demo202.insertOne({"StudentFirstName":"David","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bd803d395bdc21346e9") } > db.demo202.insertOne({"StudentFirstName":"Bob","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bde03d395bdc21346ea") }
Display all documents from a collection with the help of find() method −
> db.demo202.find();
This will produce the following output −
{ "_id" : ObjectId("5e3c3bd103d395bdc21346e8"), "StudentFirstName" : "Chris", "StudentAge" : 21 } { "_id" : ObjectId("5e3c3bd803d395bdc21346e9"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ObjectId("5e3c3bde03d395bdc21346ea"), "StudentFirstName" : "Bob", "StudentAge" : 22 }
Following is the query to display explain query plan −
> db.demo202.find({"StudentFirstName":"David"}).explain();
This will produce the following output −
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo202", "indexFilterSet" : false, "parsedQuery" : { "StudentFirstName" : { "$eq" : "David" } }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "StudentFirstName" : { "$eq" : "David" } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }
Advertisements