Database Manual / Reference / mongosh Methods / Cursors

cursor.explain() (mongosh method方法)

Definition定义

cursor.explain(verbosity)

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Provides information on the query plan for the db.collection.find() method.提供有关db.collection.find()方法的查询计划的信息。

The explain() method has the following form:explain()方法具有以下形式:

db.collection.find().explain()

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

Parameter参数Necessity必要性Type类型Description描述
verboseOptional可选String字符串

Determines the amount of information to include in the explain output. The possible verbosity modes are:确定解释输出中包含的信息量。可能的冗长模式有:

  • allPlansExecution
  • executionStats
  • queryPlanner (Default)

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

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

The explain() method returns a document with the query plan and, optionally, the execution statistics.explain()方法返回一个包含查询计划和执行统计信息(可选)的文档。

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. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关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 cursor.explain() and the amount of information returned depend on the verbosity mode.cursor.explain()的行为和返回的信息量取决于verbosity(详细程度)模式。

queryPlanner Mode模式

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

MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. MongoDB运行查询优化器,为评估中的操作选择获胜计划。cursor.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运行查询优化器以选择获胜计划,执行获胜计划直至完成,并返回描述获胜计划执行情况的统计数据。

cursor.explain() returns the queryPlanner and executionStats information for the evaluated method. cursor.explain()返回被评估方法的queryPlannerexecutionStats信息。However, executionStats does not provide query execution information for the rejected plans.但是,executionStats不提供被拒绝计划的查询执行信息。

allPlansExecution Mode模式

MongoDB runs the query optimizer to choose the winning plan and executes the winning plan to completion. MongoDB运行查询优化器来选择获胜计划,并执行获胜计划直至完成。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."allPlansExecution"模式下,MongoDB返回描述获胜计划执行情况的统计数据,以及在计划选择过程中捕获的其他候选计划的统计数据。

cursor.explain() returns the queryPlanner and executionStats information for the evaluated method. cursor.explain()返回被评估方法的queryPlannerexecutionStats信息。The executionStats includes the completed query execution information for the winning plan.executionStats包括获胜计划的已完成查询执行信息。

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.如果查询优化器考虑了多个计划,executionStats信息还包括在计划选择阶段为获胜和被拒绝的候选计划捕获的部分执行信息。

db.collection.explain().find()

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

See db.collection.explain() for more information.有关更多信息,请参阅db.collection.explain()

Output输出

cursor.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. ,从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.,其中详细说明了查询优化器选择的计划,并列出了被拒绝的计划。
  • 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. Explain output that exceeds the limit is truncated.解释输出受BSON文档的最大嵌套深度限制,即100级嵌套。解释超出限制的输出被截断。

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

Example示例

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

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