In this section, we are going to look at some tools provided by MongoDB for analyzing individual queries, and learn how to use their output for optimization.
The explain()
method is used to explain a query, giving us useful information about how the query was performed, which we could use to fine-tune the query itself. It is invoked on a cursor, and it returns a document holding pieces of data about the query.
> db.movies.find({name: ‘Inception'}).explain() { "cursor" : "BtreeCursor genre_1", "nscanned" : 3, "nscannedObjects" : 3, "n" : 3, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { "genre" : [ [ "action", "action" ] ] } } >
Let's take a look at some of the important information returned by explain():
cursor:
The value for this field could either be aBasicCursor
or aBtreeCursor
. If it is the second, it means the query has used an index. Since thegenre
field was...