对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db. 集合.explain()(mongosh方法)

带驱动程序的 MongoDB

本页面文档 mongosh 方法。要查看 MongoDB 驱动程序中的等效方法,请参阅您的编程语言的相应页面:
db.collection.explain()

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

返回有关 mapReduce() 的信息。

要使用 db.collection.explain(),请将上述方法之一附加到 db.collection.explain():

db.collection.explain().<method(...)>

例如,

db.movies.explain().remove( { rated: "PG" }, { justOne: true } )

有关更多示例,请参阅示例。 另请参阅 db.collection.explain().help()。

db.collection.explain() 方法具有以下参数:

Parameter
类型
说明

verbosity

字符串

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

  • "queryPlanner" (默认)

  • "executionStats"

  • "allPlansExecution"

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

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

注意

db.collection.explain() 默认为 queryPlanner,与默认为 allPlansExecution的 explain 命令不同。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas :用于在云中部署MongoDB 的完全托管服务

注意

所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令。

要使用 explain,您必须有权运行根本的命令。

注意

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

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

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

MongoDB运行查询优化器,为正在评估的操作选择获胜计划。db.collection.explain() 返回被评估方法的 queryPlanner 信息。

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

在版本 9.0 中进行了更改:

如果解释针对分片集群上不存在的数据库的写入操作,则该操作不会创建该数据库。

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

  • 在 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()。

8.3版本新增。

如果对分片集群上不存在的数据库运行db.collection.explain(),则执行阶段将到达流结束,并且该操作不会创建该数据库。有关流结束执行统计信息的更多信息,请参阅 explain.executionStats.executionStages.isEOF。

db.collection.explain() 操作可以返回以下信息:

  • explainVersion,输出格式版本(例如 "1")。

  • command,其中详细说明了要解释的命令。

  • queryShapeHash,从MongoDB 8.0 开始,是一个十六进制 string,具有查询结构的哈希值。有关详情,请参阅查询结构、查询结构哈希和 explain.queryShapeHash。

  • queryPlanner,其详细说明了查询优化器选择的计划,并列出了被拒绝的计划。

  • executionStats,其中详细说明了获胜计划的执行情况以及被拒计划。

  • serverInfo,提供有关 MongoDB 实例的信息。

  • serverParameters,其中详细说明了内部参数。

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

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

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

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

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

db.movies.explain().count( { year: { $gt: 2000 } } )

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

db.movies.explain("executionStats").find(
{ year: { $gt: 2000 }, rated: "PG" }
)

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

注意

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

db.movies.explain( "allPlansExecution" ).findAndModify( {
query: { rated: "PG", year: { $gt: 2000 } },
sort: { year: 1 },
update: { $inc: { "imdb.votes": 1 } }
} )

db.collection.explain().find() 构造允许链接查询修饰符。示例,以下操作提供有关带有 sort() 和hint()查询修饰符的 find() 方法的信息。

db.movies.explain("executionStats").find(
{ year: { $gt: 2000 }, rated: "PG" }
).sort( { year: -1 } ).hint( { rated: 1, year: -1 } )

有关可用查询修饰符的列表,请在 mongosh 中运行以下命令:

db.collection.explain().find().help()

db.collection.explain().find() 返回 explain() 结果。如果在 mongosh 中以交互方式运行,则可以使用 .finish() 方法访问权限查询计划:

db.movies.explain().find( { rated: "PG" } ).finish().queryPlanner.winningPlan