cursor.explain()

On this page本页内容

Definition定义

cursor.explain(verbosity)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的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参数Type类型Description描述
verbose string

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

Default mode is "queryPlanner".默认模式为"queryPlanner"

For backwards compatibility with earlier versions of cursor.explain(), MongoDB interprets true as "allPlansExecution" and false as "queryPlanner".为了向后兼容cursorexplain()的早期版本,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()方法返回一个带有查询计划的文档,还可以选择返回执行统计信息。

Behavior行为

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.cursor.explain()返回已计算方法的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. 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.

cursor.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.

db.collection.explain().find()

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

  • The db.collection.explain().find() construct allows for the additional chaining of query modifiers. For list of query modifiers, see db.collection.explain().find().help().
  • The db.collection.explain().find() returns a cursor, which requires a call to .next(), or its alias .finish(), to return the explain() results.

See db.collection.explain() for more information.

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:

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

Output输出

cursor.explain() operations can return information regarding:操作可以返回有关以下内容的信息:

The verbosity mode (i.e. queryPlanner, executionStats, allPlansExecution) determines whether the results include executionStats and whether executionStats includes data captured during plan selection.

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