显示 MongoDB 解释查询计划?
要获取查询计划的信息,请在 MongoDB 中使用 explain()。我们创建一个带有文档的集合 −
> 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") }
使用 find() 方法显示集合中的所有文档 −
> db.demo202.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e3c3bd103d395bdc21346e8"), "StudentFirstName" : "Chris", "StudentAge" : 21 } { "_id" : ObjectId("5e3c3bd803d395bdc21346e9"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ObjectId("5e3c3bde03d395bdc21346ea"), "StudentFirstName" : "Bob", "StudentAge" : 22 }
以下是显示解释查询计划的查询 −
> db.demo202.find({"StudentFirstName":"David"}).explain();
这将产生以下输出 −
{ "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 }
广告