OPTIONS

$explain

$explain

The $explain operator provides information on the query plan. It returns a document that describes the process and indexes used to return the query. This may provide useful insight when attempting to optimize a query. For details on the output, see cursor.explain().

You can specify the $explain operator in either of the following forms:

db.collection.find()._addSpecial( "$explain", 1 )
db.collection.find( { $query: {}, $explain: 1 } )

You also can specify $explain through the explain() method in the mongo shell:

db.collection.find().explain()

$explain runs the actual query to determine the result. Although there are some differences between running the query with $explain and running without, generally, the performance will be similar between the two. So, if the query is slow, the $explain operation is also slow.

Additionally, the $explain operation reevaluates a set of candidate query plans, which may cause the $explain operation to perform differently than a normal query. As a result, these operations generally provide an accurate account of how MongoDB would perform the query, but do not reflect the length of these queries.

To determine the performance of a particular index, you can use hint() and in conjunction with explain(), as in the following example:

db.products.find().hint( { type: 1 } ).explain()

When you run explain() with hint(), the query optimizer does not reevaluate the query plans.

Note

In some situations, the explain() operation may differ from the actual query plan used by MongoDB in a normal query.

The explain() operation evaluates the set of query plans and reports on the winning plan for the query. In normal operations the query optimizer caches winning query plans and uses them for similar related queries in the future. As a result MongoDB may sometimes select query plans from the cache that are different from the plan displayed using explain().

See also

←   $comment $hint  →
ON THIS PAGE