cursor.explain()

在本页面

Definition

提供有关db.collection.find()方法的查询计划的信息。

explain()方法具有以下形式:

db.collection.find().explain()

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

Parameter Type Description
verbose string 可选的。指定说明输出的详细模式。该模式影响explain()的行为并确定要返回的信息量。可能的模式是:"queryPlanner""executionStats""allPlansExecution"


默认模式为"queryPlanner"
为了与cursor.explain()的早期版本向后兼容,MongoDB 会将true解释为"allPlansExecution",而将false解释为"queryPlanner"
有关这些模式的更多信息,请参见Verbosity Modes
在版本 3.0 中更改。

explain()方法返回包含查询计划以及执行统计信息的文档。

Behavior

Verbosity Modes

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

queryPlanner Mode

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

MongoDB 运行query optimizer来为评估中的操作选择胜出计划。 cursor.explain()返回评估方法的queryPlanner信息。

executionStats Mode

MongoDB 运行query optimizer选择中奖计划,执行中奖计划直至完成,并返回描述中奖计划执行情况的统计信息。

cursor.explain()返回评估方法的queryPlannerexecutionStats信息。但是,executionStats不提供被拒绝计划的查询执行信息。

allPlansExecution Mode

MongoDB 运行query optimizer选择中奖计划,并执行中奖计划直至完成。在"allPlansExecution"模式下,MongoDB 返回描述获胜计划执行情况的统计信息以及在plan selection期间捕获的其他候选计划的统计信息。

cursor.explain()返回评估方法的queryPlannerexecutionStats信息。 executionStats包括中奖计划已完成查询执行信息。

如果查询优化器考虑了多个计划,则executionStats信息还包括在计划选择阶段期间针对获胜和被拒绝的候选计划捕获的* partial *执行信息。

db.collection.explain().find()

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

有关更多信息,请参见db.collection.explain()

Example

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

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

Output

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

详细模式(即queryPlannerexecutionStatsallPlansExecution)确定结果是否包括executionStats以及executionStats是否包括在plan selection期间捕获的数据。

有关输出的详细信息,请参见Explain Results

对于具有 3.0 mongos版本和至少一个 2.6 mongod shard 的混合版本分片群集,当您在 3.0 mongo版本的 shell 中运行cursor.explain()时,cursor.explain()将使用$explain运算符重试以 2.6 格式返回结果。

首页