https://dzone.com/articles/mongodb-index-usage-and-mongodb-explain-part-1
In this two-part series, I'm going to explain explain. No, the repetition is not a pun or a typo. I'm talking about explain(), the most useful MongoDB feature to investigate a query. Here, I'll introduce the index types available in MongoDB, their properties, and how to create and use them. In the next article, we'll see explain() in action on some real examples.
Using explain(), questions like these will find a proper answer:
- Is the query using an index?
- How is it using the index?
- How many documents are scanned?
- How many documents are returned?
- For how many milliseconds does the query run?
And many others. The explain() command provides a lot of information.
If you are familiar with MySQL, then you probably know about the command EXPLAIN. In MongoDB, we have a similar feature, and the goal of using it is exactly the same: find out how we can improve a query in order to reduce the execution time as far as possible. Based on the information provided, we can decide to create a new index or to rewrite the query to take advantage of indexes that already exist. The same as you do when using MySQL.