- Reference >
- Operators >
- Query Modifiers >
- $explain
$explain¶
On this page
-
$explain¶ Note
- Deprecated in the
mongoShell since v3.0 - Starting in v3.2, the
$explainoperator is deprecated in themongoshell. In themongoshell, usedb.collection.explain()orcursor.explain()instead.
The
$explainoperator 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
$explainoperator in either of the following forms:In the
mongoshell, you also can retrieve query plan information through theexplain()method:- Deprecated in the
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 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.
See also
explain()- MongoDB Performance page for information regarding optimization strategies.
- Database Profiler tutorial for information regarding the database profile.
- Current Operation Reporting