$explain
Deprecated since v3.0
Starting in v3.2, the $explain
operator is deprecated in themongo
shell. In the mongo
shell,use db.collection.explain()
or cursor.explain()
instead.
The $explain
operator provides information on the queryplan. It returns a document that describesthe process and indexes used to return the query. This may provideuseful insight when attempting to optimize a query.For details on the output, see cursor.explain().
You can specify the $explain
operator in either of thefollowing forms:
- db.collection.find()._addSpecial( "$explain", 1 )
- db.collection.find( { $query: {}, $explain: 1 } )
In the mongo
shell, you also can retrieve query planinformation through the explain()
method:
- db.collection.find().explain()
Behavior
$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 performancewill be similar between the two. So, if the query is slow, the$explain
operation is also slow.
Additionally, the $explain
operation reevaluates a setof 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 ofthese queries.
See also
explain()
- MongoDB Performance page for informationregarding optimization strategies.
- Database Profiler tutorial forinformation regarding the database profile.
- Current Operation Reporting