> db.users.createIndex({gender:1, name:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }在 mongo shell 中,您可以通过 $explain 的辅助方法 explain() 来检索查询的相关信息:
> db.users.find({gender:"M"}, {name:1,_id:0}).explain()上面的 explain() 查询会返回以下分析结果:
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "bianchengbang.users", "indexFilterSet" : false, "parsedQuery" : { "gender" : { "$eq" : "M" } }, "winningPlan" : { "stage" : "PROJECTION", "transformBy" : { "name" : 1, "_id" : 0 }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "gender" : 1, "name" : 1 }, "indexName" : "gender_1_name_1", "isMultiKey" : false, "multiKeyPaths" : { "gender" : [ ], "name" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "gender" : [ "[\"M\", \"M\"]" ], "name" : [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "LAPTOP-MDE57TIS", "port" : 27017, "version" : "4.0.10", "gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766" }, "ok" : 1 }关于上面的运行结果,有以下几点需要说明:
> db.users.find({gender:"M"},{name:1, _id:0}).hint({gender:1, name:1}) { "name" : "bianchengbang" }使用 explain() 来分析以上查询:
> db.users.find({gender:"M"},{name:1, _id:0}).hint({gender:1, name:1}).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "bianchengbang.users", "indexFilterSet" : false, "parsedQuery" : { "gender" : { "$eq" : "M" } }, "winningPlan" : { "stage" : "PROJECTION", "transformBy" : { "name" : 1, "_id" : 0 }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "gender" : 1, "name" : 1 }, "indexName" : "gender_1_name_1", "isMultiKey" : false, "multiKeyPaths" : { "gender" : [ ], "name" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "gender" : [ "[\"M\", \"M\"]" ], "name" : [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "LAPTOP-MDE57TIS", "port" : 27017, "version" : "4.0.10", "gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766" }, "ok" : 1 }
本文链接:http://task.lmcjl.com/news/17688.html