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. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.

Returns information on the query plan for the following methods:

Starting in MongoDB 3.0
Starting in MongoDB 3.2
Starting in MongoDB 4.4

To use db.collection.explain(), append one of the aforementioned methods to db.collection.explain():


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:


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"

For backwards compatibility with earlier versions of cursor.explain(), MongoDB interprets true as "allPlansExecution" and false as "queryPlanner".

For more information on the modes, see Verbosity Modes.

The behavior of db.collection.explain() and the amount of information returned depend on the verbosity mode.

For write operations, db.collection.explain() returns information about the write operation that would be performed but does not actually modify the database.

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:

  • run the explain in queryPlanner mode or
  • 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.

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:

db.collection.explain().aggregate() is equivalent to passing the explain option to the db.collection.aggregate() method.

To see the list of operations supported by db.collection.explain(), run:


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:


You can chain multiple modifiers to db.collection.explain().find(). For an example, see Explain find() with Modifiers.

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 } } )

The following example runs db.collection.explain() in "executionStats" verbosity mode to return the query planning and execution information for the specified find() operation:

{ quantity: { $gt: 50 }, category: "apparel" }

The following example runs db.collection.explain() in "allPlansExecution" verbosity mode. The db.collection.explain() returns the queryPlanner and executionStats for all considered plans for the specified update() operation:


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.

{ quantity: { $lt: 1000}, category: "apparel" },
{ $set: { reorder: true } }

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.

{ quantity: { $gt: 50 }, category: "apparel" }
).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } )

For a list of query modifiers available, run in the mongo shell:


db.collection.explain().find() returns a cursor to the explain results. If run interactively in the mongo shell, the mongo shell 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();

db.collection.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.

Give Feedback

On this page

  • Description
  • Behavior
  • Examples
  • Output