MongoDB explain() explained
Tags: mongodb
Published: 2017-05-22 13:54:26

---
title: MongoDB explain() explained
published: true
description: A brief overview of MongoDB's explain() method
tags: MongoDB
---
There are many different considerations to be made when running queries in <a href="https://www.mongodb.com">MongoDB</a>. A helpful thing to use in the <a href="https://docs.mongodb.com/getting-started/shell/client/">mongo shell</a> when running a <a href="https://docs.mongodb.com/manual/reference/method/db.collection.find/">find()</a> operation is to use the <a href="https://docs.mongodb.com/manual/reference/method/cursor.explain/">explain()</a> method. In this blog post I'll take a look at some of the options for explain() and what the results mean.
<h3>explain()</h3>
As discussed in a previous post on <a href="https://www.kenwalger.com/blog/nosql/mongodb/indexing-in-mongodb/">indexing</a> in MongoDB, we can use the explain() method to learn about the selected query plan. This allows for an examination of the performance of a given query. It can be used in the following manner:

```javascript
db.collection.find().explain()
```


The information generated can be used to see what index is being used for a query, if the query is a covered query, Â and which servers in a <a href="https://docs.mongodb.com/manual/sharding/">sharded collection</a> the query is run against, to name a few.

Three different verbosity modes can be utilized to determine the amount of information provided.
<h5>Verbosity modes</h5>
<ul>
 	<li><strong>queryPlanner</strong> - the given query provided in the find() method is put through the <a href="https://docs.mongodb.com/manual/core/query-plans/">query optimizer</a> to find the most efficient query. This "winning plan" is then passed to the <a href="https://docs.mongodb.com/manual/reference/explain-results/#explain.queryPlanner">queryPlanner </a>and the information is returned for the evaluated query. The query is not run in this mode. As a result things like query time, e.g. <code>executionTimeMillisEstimate</code> are true estimates since the query has not been executed.</li>
 	<li><strong>executionStats</strong> - when running in this mode, the query optimizer is run and the query is fully executed. The information returned details the results of the are what actually happened during that specific query.</li>
 	<li><strong>allPlansExecution</strong> - as the name might suggest, this mode returns information about all possible query plans. While the winning plan is executed and statistics returned for it, other candidate plan information is returned as well. This is the default mode of explain().</li>
</ul>
The variety of information these different modes provides can be extremely useful. Let's take a look at some returned results of explain() and walk through what they show.
<h3>Results</h3>
For this example I will use a test example database of a blog. The database contains two collections,Â <em>users</em> andÂ <em>articles</em>, and is running on a single, unsharded, machine. Each collection has, roughly, 550,500 documents and is not indexed beyond the index for <code>_id</code>.

Let's start with looking at what gets returned from a query for a single username. And take a look at some of the bits and pieces of information provided.

```javascript
db.users.find( { "username": "User_9"} ).explain()
```

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/basic_find_with_notes-1.png"><img class="alignnone size-full wp-image-298" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/basic_find_with_notes-1.png" alt="explain output" width="600" height="358" /></a>

The parsedQuery section is the query we are exploring. The queryÂ <em>stage</em> provides for a description of the type of operation that occurred for the winning plan. It can be one of the following:
<ul>
 	<li><strong>COLLSCAN</strong> - indicates a collection scan occurred for the query, meaning that the query looked atÂ <strong>each</strong> document to get the results</li>
 	<li><strong>IXSCAN</strong> - indicates an index was used for the query</li>
 	<li><strong>FETCH</strong> - for retrievingÂ documents</li>
 	<li><strong>SHARD_MERGE</strong> - the result of merging data from shards</li>
</ul>
The stage is a tree structure and can have multiple, child, stages. TheÂ <em>direction</em> of the query shows whether the query was performed in a forward or reverse order. TheÂ <em>serverInfo</em> section displays information on the server the query was run against and includes, in theÂ <em>version</em> key, the version of the MongoDB database. If the collection was in a sharded environment, each accessed shard would be listed in theÂ <em>serverInfo</em>.

When the command is run using the "executionStats" verbosity mode:

```javascript
db.users.find({ "username": "User_9"} ).explain("executionStats")
```

additional information is provided as a result of the query being run on the data.

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/executionStats_with_notes.png"><img class="alignnone size-full wp-image-299" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/executionStats_with_notes.png" alt="explain with executionStats" width="600" height="631" /></a>

Here we see, among other things, the time the query took to run, along with how many documents were returned, <code>nReturned</code>, and how many documents were examined by the database, <code>totalDocsExamined</code>. As mentioned in my post on <a href="https://www.kenwalger.com/blog/nosql/mongodb/indexing-in-mongodb/">indexing</a>, ideally these two numbers should be very close to the same value.
<h3>Wrap Up</h3>
There is a lot of information available when using the explain() method. It provides some great information about how queries areÂ <em>actually<strong>Â </strong></em>being run and gives an indication as to where a collection can benefit from an index. It should be your first stop when examining slow queries before moving onto other <a href="https://www.kenwalger.com/blog/nosql/mongodb-cli-tools/">MongoDB tools</a>.

<hr />


<em>Follow me on Twitter <a href="https://www.twitter.com/kenwalger">@kenwalger</a> to get the latest updates on my postings, or see the original post on my <a href="https://www.kenwalger.com/blog" target="_blank">blog</a>.</em>