带驱动程序的 MongoDB
本页介绍了mongosh 方法。要查看MongoDB驾驶员中的等效方法,请参阅您的编程语言的相应页面:
说明
- db.collection.explain()
- 返回以下方法的查询计划信息: - 返回有关 - mapReduce()的信息。- 要使用 - db.collection.explain(),请将上述方法之一附加到- db.collection.explain():- db.collection.explain().<method(...)> - 例如, - db.products.explain().remove( { category: "apparel" }, { justOne: true } ) - 有关更多示例,请参阅示例。另请参阅 db.collection.explain().help()。 - db.collection.explain()方法具有以下参数:Parameter类型说明- verbosity- 字符串 - 可选。指定解释输出的详细模式。该模式会影响 - explain()的行为并确定要返回的信息量。可能的模式:- "queryPlanner"(默认)
- "executionStats"
- "allPlansExecution"
 - 为实现与早期版本的 - cursor.explain()的向后兼容,MongoDB 将- true解释为- "allPlansExecution",并将- false解释为- "queryPlanner"。
行为
Verbosity 模式
db.collection.explain() 的行为和返回的信息量取决于 verbosity 模式。
默认情况下,db.collection.explain() 以 queryPlanner 详细模式运行。
MongoDB 运行查询优化器,以便为正在接受评估的操作选择优胜计划。db.collection.explain() 返回已评估方法的 queryPlanner 信息。
MongoDB 运行查询优化器来选择获胜计划并执行获胜计划直至完成,并返回描述获胜计划执行情况的统计信息。
对于写入操作,db.collection.explain() 会返回有关将执行的更新或删除操作的信息,但不会将修改应用到数据库中。
db.collection.explain() 返回被评估方法的 queryPlanner 和 executionStats 信息。但是,executionStats 并未提供被拒绝计划的查询执行信息。
MongoDB 运行查询优化器来选择优胜计划并执行该计划直至完成。在 "allPlansExecution" 模式下,MongoDB 返回描述优胜计划执行情况的统计信息以及在计划选择期间捕获的其他候选计划的统计信息。
对于写入操作,db.collection.explain() 会返回有关将执行的更新或删除操作的信息,但不会将修改应用到数据库中。
db.collection.explain() 返回被评估方法的 queryPlanner 和 executionStats 信息。executionStats 包含优胜计划的已完成查询执行信息。
如果查询优化器考虑了多个计划,executionStats 信息则还包括在计划选择阶段为获胜和被拒计划收集的部分执行信息。
解释和写入操作
对于写入操作,db.collection.explain() 会返回将要执行的写入操作的相关信息,但不会实际修改数据库。
限制
对于包含 $out 阶段的 aggregation pipeline,不能在 executionStats 模式或 allPlansExecution 模式下运行 explain 命令/db.collection.explain()。取而代之的是,您可以:
explain() 机制
db.collection.explain() 方法包装 explain 命令,是运行 explain 的首选方式。
db.collection.explain().find() 与 db.collection.find().explain() 相似,主要区别如下:
- db.collection.explain().find()结构允许额外链接查询修饰符。要获取查询修饰符的列表,请参阅 db.collection.explain().find().help()。
- db.collection.find().explain()返回一个游标,需要调用- .next()或其别名- .finish()才能返回- explain()结果。如果在- mongosh中交互运行,- mongosh会自动调用- .finish()以返回结果。但是,对于脚本,您必须显式调用- .next()或- .finish()才能返回结果。有关游标相关方法的列表,请参阅 db.collection.explain().find().help()。
db.collection.explain().aggregate() 等同于将解释选项传递给 db.collection.aggregate() 方法。
help()
要查看 db.collection.explain() 支持的操作列表,请运行:
db.collection.explain().help() 
db.collection.explain().find() 将返回一个游标,从而允许链接查询修饰符。要查看 db.collection.explain().find() 支持的查询修饰符的列表以及与游标相关的方法,请运行:
db.collection.explain().find().help() 
您可以将多个修饰符链接到 db.collection.explain().find()。有关示例,请参阅使用修饰符解释 find()。
输出
db.collection.explain() 操作可以返回以下信息:
- explainVersion,输出格式版本(例如,- "1");
- command,其中详细说明了要解释的命令;
- queryPlanner,其中详细说明了查询优化器选择的计划,并列出了被拒绝的计划;
- executionStats,其中详细说明了获胜计划的执行情况以及被拒计划;
- serverInfo,提供有关 MongoDB 实例的信息;以及
- serverParameters,其中详细说明了内部参数。
详细模式(例如 queryPlanner、executionStats、allPlansExecution )决定了结果是否包括 executionStats,以及 executionStats 是否包括计划选择期间捕获的数据。
有关输出的详细信息,请参阅解释结果。
示例
queryPlanner 模式
默认情况下,db.collection.explain() 以 "queryPlanner" 详细模式运行。
以下示例在 "queryPlanner" 详细模式下运行 db.collection.explain(),以返回指定 count() 操作的查询计划信息:
db.products.explain().count( { quantity: { $gt: 50 } } ) 
executionStats  模式
以下示例在 "executionStats" 详细模式下运行 db.collection.explain(),以返回指定 find() 操作的查询规划和执行信息:
db.products.explain("executionStats").find(    { quantity: { $gt: 50 }, category: "apparel" } ) 
allPlansExecution 模式
以下示例在 "allPlansExecution" 详细模式下运行 db.collection.explain()。db.collection.explain() 返回指定 findAndModify() 操作的所有考虑计划的 queryPlanner 和 executionStats:
注意
执行该解释不会修改数据,但会运行更新操作的查询谓词。对于候选计划,MongoDB 会返回在计划选择阶段捕获的执行信息。
db.products.explain( "allPlansExecution" ).findAndModify( {    query: { name: "Tom", state: "active", rating: { $gt: 10 } },    sort: { rating: 1 },    update: { $inc: { score: 1 } } } ) 
find()用修饰符解释
db.collection.explain().find() 构造允许对查询修饰符进行链式调用。例如,以下操作提供带 find() 和 sort()hint() 查询修饰符的方法的信息。
db.products.explain("executionStats").find(    { quantity: { $gt: 50 }, category: "apparel" } ).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } ) 
有关可用查询修饰符的列表,请在 mongosh 中运行以下命令:
db.collection.explain().find().help() 
explain().find()迭代 返回游标
db.collection.explain().find() 将返回用于解释结果的游标。如果在 mongosh 中以交互方式运行,mongosh 则会使用 .next() 方法自动迭代此游标。但是,对于脚本,您必须显式调用 .next()(或其别名 .finish())才能返回结果:
var explainResult = db.products.explain().find( { category: "apparel" } ).next();