Database Manual / Reference / mongosh Methods / Collections

db.collection.explain() (mongosh method方法)

Description

db.collection.explain()

Returns information on the query plan for the following methods:返回以下方法的查询计划信息:

Returns information on mapReduce().返回有关mapReduce()的信息。

To use db.collection.explain(), append one of the aforementioned methods to db.collection.explain():要使用db.collection.explain(),请将上述方法之一附加到db.collection.explain()

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

For example,例如,

db.products.explain().remove( { category: "apparel" }, { justOne: true } )

For more examples, see Examples. See also db.collection.explain().help().有关更多示例,请参阅示例。另请参见db.collection.explain().help()

The db.collection.explain() method has the following parameter:db.collection.explain()方法有以下参数:

Parameter参数Type类型Description描述
verbositystring字符串

Optional. 可选。Specifies the verbosity mode for the explain output. The mode affects the behavior of explain() and determines the amount of information to return. The possible modes are:指定解释输出的详细程度模式。模式影响explain()的行为,并决定返回的信息量。可能的模式有:

  • "queryPlanner" (Default)
  • "executionStats"
  • "allPlansExecution"

For backwards compatibility with earlier versions of cursor.explain(), MongoDB interprets true as "allPlansExecution" and false as "queryPlanner".为了与早期版本的cursor.explain()向后兼容,MongoDB将true解释为"allPlansExecution",将false解释为"queryPlanner"

For more information on the modes, see Verbosity Modes.有关模式的更多信息,请参阅详细模式

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

To use explain, you must have permission to run the underlying command.要使用explain,您必须具有运行底层命令的权限。

Behavior行为

Note

Using explain ignores all existing plan cache entries and prevents the MongoDB query planner from creating a new plan cache entry.使用explain会忽略所有现有的计划缓存条目,并阻止MongoDB查询计划器创建新的计划缓存条目的操作。

Verbosity Modes详细模式

The behavior of db.collection.explain() and the amount of information returned depend on the verbosity mode.db.collection.explain()的行为和返回的信息量取决于verbosity模式。

queryPlanner Mode(Default)

By default, db.collection.explain() runs in queryPlanner verbosity mode.默认情况下,db.collection.explain()queryPlanner详细程度模式下运行。

MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. MongoDB运行查询优化器,为评估中的操作选择获胜计划。db.collection.explain() returns the queryPlanner information for the evaluated method.返回所评估方法的queryPlanner信息。

executionStats Mode

MongoDB runs the query optimizer to choose the winning plan, executes the winning plan to completion, and returns statistics describing the execution of the winning plan.MongoDB运行查询优化器以选择获胜计划,执行获胜计划直至完成,并返回描述获胜计划执行情况的统计数据。

For write operations, db.collection.explain() returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.对于写操作,db.collection.explain()返回有关将要执行的更新或删除操作的信息,但不会将修改应用于数据库。

db.collection.explain() returns the queryPlanner and executionStats information for the evaluated method. However, executionStats does not provide query execution information for the rejected plans.

allPlansExecution Mode

MongoDB runs the query optimizer to choose the winning plan and executes the winning plan to completion. In "allPlansExecution" mode, MongoDB returns statistics describing the execution of the winning plan as well as statistics for the other candidate plans captured during plan selection.

For write operations, db.collection.explain() returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.

db.collection.explain() returns the queryPlanner and executionStats information for the evaluated method. The executionStats includes the completed query execution information for the winning plan.

If the query optimizer considered more than one plan, executionStats information also includes the partial execution information captured during the plan selection phase for both the winning and rejected candidate plans.

Explain and Write Operations解释和编写操作

For write operations, db.collection.explain() returns information about the write operation that would be performed but does not actually modify the database.对于写操作,db.collection.explain()返回有关将要执行但实际上不会修改数据库的写操作的信息。

Restrictions限制

You cannot run the explain command/db.collection.explain() in executionStats mode or allPlansExecution mode for an aggregation pipeline that contains the $out stage. Instead, you can either:对于包含$out阶段的聚合管道,您无法在executionStats模式或allPlansExecution模式下运行explain命令/db.collection.explain()。相反,您可以:

  • run the explain in queryPlanner mode orqueryPlanner模式下运行解释,或
  • run the explain in executionStats mode or allPlansExecution mode but without the $out stage to return information for the stages that precede the $out stage.executionStats模式或allPlansExecution模式下运行解释,但不运行$out阶段,以返回$out阶段之前阶段的信息。

explain() Mechanics机制

The db.collection.explain() method wraps the explain command and is the preferred way to run explain.db.collection.explain()方法封装了explain命令,是运行explain的首选方式。

db.collection.explain().find() is similar to db.collection.find().explain() with the following key differences:db.collection.find().explain()类似,但有以下关键区别:

db.collection.explain().aggregate() is equivalent to passing the explain option to the db.collection.aggregate() method.相当于将解释选项传递给db.collection.aggregate()方法。

help()

To see the list of operations supported by db.collection.explain(), run:要查看db.collection.explain()支持的操作列表,请运行:

db.collection.explain().help()

db.collection.explain().find() returns a cursor, which allows for the chaining of query modifiers. 返回一个游标,它允许链接查询修饰符。To see the list of query modifiers supported by db.collection.explain().find() as well as cursor-related methods, run:要查看db.collection.explain().find()支持的查询修饰符列表以及与游标相关的方法,请运行:

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

You can chain multiple modifiers to db.collection.explain().find(). For an example, see Explain find() with Modifiers.您可以将多个修饰符链接到db.collection.explain().find()。例如,请参阅使用修饰符解释find()

Output输出

db.collection.explain() operations can return information regarding:操作可以返回以下信息:

  • explainVersion, the output format version (for example, "1").,输出格式版本(例如"1")。
  • command, which details the command being explained.,其中详细说明了正在解释的命令。
  • queryShapeHash, starting in MongoDB 8.0, which is a hexadecimal string with the hash of a query shape. queryShapeHash,从MongoDB 8.0开始,它是一个十六进制字符串,具有查询形状的哈希值。For details, see Query Shapes, Query Shape Hash, and explain.queryShapeHash.有关详细信息,请参阅查询形状查询形状哈希explain.queryShapeHash
  • queryPlanner, which details the plan selected by the query optimizer and lists the rejected plans.queryPlanner,详细说明查询优化器选择的计划并列出被拒绝的计划。
  • executionStats, which details the execution of the winning plan and the rejected plans.,其中详细说明了获胜计划和被拒绝计划的执行情况。
  • serverInfo, which provides information on the MongoDB instance.,它提供有关MongoDB实例的信息。
  • serverParameters, which details internal parameters.,其中详细说明了内部参数。

The verbosity mode (i.e. queryPlanner, executionStats, allPlansExecution) determines whether the results include executionStats and whether executionStats includes data captured during plan selection.详细程度模式(即queryPlannerexecutionStatsallPlansExecution)确定结果是否包括executionStats,以及executionStats是否包括在计划选择过程中捕获的数据。

Explain output is limited by the maximum Nested Depth for BSON Documents, which is 100 levels of nesting. 解释输出受BSON文档的最大嵌套深度限制,即100级嵌套。Explain output that exceeds the limit is truncated.解释超出限制的输出被截断。

For details on the output, see Explain Results.有关输出的详细信息,请参阅解释结果

Examples示例

queryPlanner Mode模式

By default, db.collection.explain() runs in "queryPlanner" verbosity mode.默认情况下,db.collection.explain()"queryPlanner"详细模式下运行。

The following example runs db.collection.explain() in "queryPlanner" verbosity mode to return the query planning information for the specified count() operation:以下示例在"queryPlanner"详细程度模式下运行db.collection.explain(),以返回指定count()操作的查询计划信息:

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

executionStats Mode模式

The following example runs db.collection.explain() in "executionStats" verbosity mode to return the query planning and execution information for the specified find() operation:以下示例在"executionStats"详细程度模式下运行db.collection.explain(),以返回指定find()操作的查询计划和执行信息:

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

allPlansExecution Mode模式

The following example runs db.collection.explain() in "allPlansExecution" verbosity mode. 以下示例在"allPlansExecution"详细模式下运行db.collection.explain()db.collection.explain() returns the queryPlanner and executionStats for all considered plans for the specified findAndModify() operation:db.collection.explain()为指定的findAndModify()操作返回所有考虑的计划的queryPlannerexecutionStats

Note

The execution of this explain will not modify data but runs the query predicate of the update operation. For candidate plans, MongoDB returns the execution information captured during the plan selection phase.执行此解释不会修改数据,而是运行更新操作的查询谓词。对于候选计划,MongoDB返回在计划选择阶段捕获的执行信息。

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

Explain find() with Modifiers用修饰符解释find()

db.collection.explain().find() construct allows for the chaining of query modifiers. 构造允许链接查询修饰符。For example, the following operation provides information on the find() method with sort() and hint() query modifiers.例如,以下操作提供了有关带有sort()hint()查询修饰符的find()方法的信息。

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

For a list of query modifiers available, run the following in mongosh:有关可用查询修饰符的列表,请在mongosh中运行以下命令:

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

Access the explain() Results with finish() Method使用finish()方法访问explain()结果

The db.collection.explain().find() returns the explain() results. db.collection.explain().find()返回explain()结果。If run interactively in mongosh you can use the .finish() method to access the query plan:如果在mongosh中以交互方式运行,则可以使用.finish()方法访问查询计划:

db.products.explain().find( { category: "apparel" } ).finish().queryPlanner.winningPlan;