24
votes

I cannot get the exact difference between "nscanned" and "nscannedObjects" in the Mongodb's explain query output.

On MongoDB Explain documentation I can read:

nscanned Number of items (documents or index entries) examined. Items might be objects or index keys. If a "covered index" is involved, nscanned may be higher than nscannedObjects.

nscannedObjects Number of documents scanned.

What's the different between these two fields? And more specific what does exactly mean when I have a query, which uses a BtreeCursor (an index), and these two fields have two different values, for example:

{
    "cursor" : "BtreeCursor a_1_b_1",
    "isMultiKey" : false,
    "n" : 5,
    "nscannedObjects" : 5,
    "nscanned" : 9, 
    (...)
}

I know what a "covered index" is. I would like to understand exactly what the query did in the example above. Did it pass through ("scanned") 9 elements (nscanned = 9), where all of them are index entries and read ("examined") the value of only 5 of them (nscannedObjects = 5) to produce the result set?

2

2 Answers

26
votes

This means that :
The query returned 5 documents - n
scanned 9 documents from the index - nscanned
and then read 5 full documents from the collection - nscannedObjects

Similar example is given at :
http://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explain

1
votes

if "cursor" is Index ==> nscanned = no. of index scanned else if "cursor" is FullTableScan ==> nscanned = no. of document scanned

nscannedObjects ==> No. of document scanned

when querying, try to minimise all count, i.e. nscanned and nscannedObjects both are minimum means your query should run faster!