执行计划之前发了一篇关于mongodb执行计划的说明。利用执行计划,我们可以判断每一次sql的执行情况和mongodb给出的执行建议。在mongo shell中跑执行计划的命令,举个例子:db.collecitonName.find({}).explain(“queryPlanner”)执行计划的模式为三种:queryPlanner executionStats allPlansExecution。第一种不会真正跑命令本身,只有响应命令分析后的报告。上面例子的响应结果就是对 db.collecitonName.find({}) 这个查询语句的分析。程序中跑执行计划我使用的是java, mongodb库用的是mongodb-java-driver。mongodb-java-driver的API提供了两种方式去跑执行计划:方式一:MongoClient mongoClient = new MongoClient(new ServerAddress(host, port));mongoClient.getDB(“xxx”).getCollection(“yyy”).find(quert).explain();这是一个便捷的方式。这种方式会真正执行命令,也就是说它使用的是executionStats模式。响应结果会有执行时间、扫描记录数等真实的执行情况。如果你的程序想要在命令执行前做一个预判,这个API不是你想要的。方式二:API没有提供queryPlanner的方式。我花了一些时间去搜索资料,发现网上没有跑queryPlanner的需求,至少我是没有找到类似的发问和使用例子。纠结了一会儿,最终发现库里有这样一个api, mongoClient.getDB(“xxx”).command(BasicDBObject command),支持程序传入一个命令。最后在官方文档里找到了这样一个说明:explainNew in version 3.0.The explain command provides information on the execution of the following commands: aggregate, count, distinct, group, find, findAndModify, delete, and update.Although MongoDB provides the explain command, the preferred method for running explain is to use the db.collection.explain() and cursor.explain() helpers.The explain command has the following syntax:语法如下:{ explain: <command>, verbosity: <string>}explain: <command>。 支持 aggregate, count, distinct, group, find, findAndModify, delete, and update等等的命令。verbosity: <string>。支持模式"queryPlanner" 、“executionStats” 、“allPlansExecution” (Default)跟踪find进去,find支持的字段如下,应有尽有。{ “find”: <string>, “filter”: <document>, “sort”: <document>, “projection”: <document>, “hint”: <document or string>, “skip”: <int>, “limit”: <int>, “batchSize”: <int>, “singleBatch”: <bool>, “comment”: <string>, “maxScan”: <int>, // Deprecated in MongoDB 4.0 “maxTimeMS”: <int>, “readConcern”: <document>, “max”: <document>, “min”: <document>, “returnKey”: <bool>, “showRecordId”: <bool>, “tailable”: <bool>, “oplogReplay”: <bool>, “noCursorTimeout”: <bool>, “awaitData”: <bool>, “allowPartialResults”: <bool>, “collation”: <document>}通过阅读文档,跑queryPlanner模式的执行计划应该是这样的:MongoClient mongoClient = MongoUtil.getConnection(mongodb.getHost(), mongodb.getPort(), “”, “”, mongodb.getDb());BasicDBObject command = new BasicDBObject();BasicDBObject find = new BasicDBObject();find.put(“find”, “集合名”);find.put(“filter”, queryCondition);//查询条件,是一个BasicDBObjectcommand.put(“explain”, find);command.put(“verbosity”, “queryPlanner”);CommandResult explainResult = mongoClient.getDB(mongodb.getDb()).command(command);