Display MongoDB explain query plan?

MongoDBDatabaseBig Data Analytics

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
}
raja
Updated on 27-Mar-2020 10:28:00

Advertisements