“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

db.collection.explain()

在此页面上

  • 说明
  • 行为
  • 输出
  • 举例
db.collection.explain()

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。

有关数据库命令,请参阅 explain命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

对于传统 mongo Shell 文档,请参阅相应 MongoDB Server 版本的文档:

mongo shell v4.4

返回以下方法的查询计划信息:

返回有关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() 方法具有以下参数:

参数
类型
说明
verbosity
字符串

可选。指定解释输出的详细模式。该模式会影响 explain() 的行为并确定要返回的信息量。可能的模式:

  • "queryPlanner" (默认)

  • "executionStats"

  • "allPlansExecution"

为实现与早期版本的 cursor.explain() 的向后兼容,MongoDB 将 true 解释为 "allPlansExecution",并将 false 解释为 "queryPlanner"

有关这些模式的更多信息,请参阅详细模式

注意

使用 explain 会忽略所有现有的计划缓存条目,并防止 MongoDB 查询计划器创建新的计划缓存条目。

db.collection.explain() 的行为和返回的信息量取决于 verbosity 模式。

对于写入操作,db.collection.explain() 会返回将要执行的写入操作的相关信息,但不会实际修改数据库。

您无法在explain db.collection.explain()executionStats模式或allPlansExecution 模式下为包含 阶段的 运行aggregation pipeline $out命令/ 。相反,您可以:

  • queryPlanner 模式下运行 explain,或者

  • executionStats模式或 allPlansExecution 模式下运行解释,但排除 $out 阶段,以返回 $out 阶段之前的阶段信息。

db.collection.explain()方法封装了explain命令,是运行explain的首选方法。

db.collection.explain().find()db.collection.find().explain()类似,但主要区别如下:

db.collection.explain().aggregate()相当于将解释选项传递给db.collection.aggregate()方法。

要查看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,其中详细说明了内部参数。

详细模式(例如 queryPlannerexecutionStatsallPlansExecution )决定了结果是否包括 executionStats,以及 executionStats 是否包括计划选择期间捕获的数据。

解释输出会受到 BSON 文档的最大嵌套深度的限制,即 100 级嵌套。解释超出限制的输出会被截断。

有关输出的详细信息,请参阅解释结果

默认情况下,db.collection.explain()"queryPlanner" 详细模式运行。

以下示例在"queryPlanner"详细模式下运行db.collection.explain() ,以返回指定count()操作的查询计划信息:

db.products.explain().count( { quantity: { $gt: 50 } } )

以下示例在"executionStats"详细模式下运行db.collection.explain() ,以返回指定find()操作的查询计划和执行信息:

db.products.explain("executionStats").find(
{ quantity: { $gt: 50 }, category: "apparel" }
)

以下示例在 "allPlansExecution" 详细模式下运行 db.collection.explain()db.collection.explain() 返回指定 findAndModify() 操作的所有考虑计划的 queryPlannerexecutionStats

注意

执行该解释不会修改数据,但会运行更新操作的查询谓词。对于候选计划,MongoDB 会返回在计划选择阶段捕获的执行信息。

db.products.explain( "allPlansExecution" ).findAndModify( {
query: { name: "Tom", state: "active", rating: { $gt: 10 } },
sort: { rating: 1 },
update: { $inc: { score: 1 } }
} )

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()

db.collection.explain().find() 将返回用于解释结果的游标。如果在 mongosh 中以交互方式运行,mongosh 则会使用 .next() 方法自动迭代此游标。但是,对于脚本,您必须显式调用 .next()(或其别名 .finish())才能返回结果:

var explainResult = db.products.explain().find( { category: "apparel" } ).next();
← db.collection.estimatedDocumentCount()