db.collection.explain()
On this page
Description
db.collection.explain()
Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
explain
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Returns information on the query plan for the following methods:
New in version 4.4: Returns information on
mapReduce()
.To use
db.collection.explain()
, append one of the aforementioned methods todb.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().
The
db.collection.explain()
method has the following parameter:Parameter Type Description verbosity
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. The possible modes are:"queryPlanner"
(Default)"executionStats"
"allPlansExecution"
cursor.explain()
, MongoDB interpretstrue
as"allPlansExecution"
andfalse
as"queryPlanner"
.
For more information on the modes, see Verbosity Modes.
Behavior
Verbosity Modes
The behavior of db.collection.explain()
and the amount of information returned depend on the verbosity
mode.
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.
Restrictions
Starting in MongoDB 4.2, 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:
explain()
Mechanics
The db.collection.explain()
method wraps the explain
command and is the preferred way to run explain
.
db.collection.explain().find()
is similar to db.collection.find().explain()
with the following key differences:
-
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.find().explain()
returns a cursor, which requires a call to.next()
, or its alias.finish()
, to return theexplain()
results. If run interactively inmongosh
,mongosh
automatically calls.finish()
to return the results. For scripts, however, you must explicitly call.next()
, or.finish()
, to return the results. For list of cursor-related methods, see db.collection.explain().find().help().
db.collection.explain().aggregate()
is equivalent to passing the explain option to the db.collection.aggregate()
method.
help()
To see the list of operations supported by db.collection.explain()
, run:
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().help()
You can chain multiple modifiers to db.collection.explain().find()
. For an example, see Explain find()
with Modifiers.
Output
db.collection.explain()
operations can return information regarding:
-
explainVersion
, the output format version (for example,"1"
); -
command
, which details the command being explained; -
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; and -
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.
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.
For details on the output, see Explain Results.
Examples
queryPlanner
Mode
By default, db.collection.explain()
runs in "queryPlanner"
verbosity mode.
The following example runs db.collection.explain()
in "queryPlanner"
verbosity mode to return the query planning information for the specified count()
operation:
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:
db.products.explain("executionStats").find( { quantity: { $gt: 50 }, category: "apparel" } )
allPlansExecution
Mode
The following example runs db.collection.explain()
in "allPlansExecution"
verbosity mode. db.collection.explain()
returns the queryPlanner
and executionStats
for all considered plans for the specified findAndModify()
operation:
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.
db.products.explain( "allPlansExecution" ).findAndModify( { query: { name: "Tom", state: "active", rating: { $gt: 10 } }, sort: { rating: 1 }, update: { $inc: { score: 1 } } } )
Explain find()
with Modifiers
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.
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
:
db.collection.explain().find().help()
Iterate the explain().find()
Return Cursor
db.collection.explain().find()
returns a cursor to the explain results. If run interactively in mongosh
, mongosh
automatically iterates the cursor using the .next()
method. For scripts, however, you must explicitly call .next()
(or its alias .finish()
) to return the results:
var explainResult = db.products.explain().find( { category: "apparel" } ).next();