Discussion:
[mongodb-dev] Mongo explain query behaviour
Rubal Jabbal
2018-11-06 06:56:29 UTC
Permalink
I am on MongoDB shell version: 3.0.15

I have been analysing a query and I didn't understand the specific behaviour

For:

db.testcollection.find( {"state" : 1}).explain(true)
the keysexamined and nreturned are correct: 2568 docs

but for db.testcollection.find( {"state" : 1}).limit(1000).explain(true)
the nreturned becomes 101 and keysexamined 102

for db.testcollection.find({"state" : 1, "all" :
1}).batchsize(1000).explain(true) shows nreturned and nexamined 1000 docs


Without an index, a collscan is hit and returns 101 docs in nreturned.
If proper index is added it hits for this query and ixscan is done with
nreturned still 101

From my understanding and also from
https://docs.mongodb.com/v3.0/core/cursors/index.html, I wouldn't have been
surprised if without limit it returned me the default doc count of 101, but
here, with limit it is returning me 101 docs after restriction instead of
2568 docs, and when no limit it is provided, it returns 101 docs in the
explain query.

From what I could gather the query would "Return at most *n* documents, but
only return a single batch, and use the default batchSize." Since the
default batchSize for the initial find command response is 101, the number
of documents returned should be 101 in the case that *n* >= 101.In versions
of the server before 3.2.0, there was no differentiation between batchSize
and limit; the two were passed to the server using a single field called "
ntoreturn". but this is applicable from mongo 3.2.0 and above, Can someone
help me out with the explanation for this behavior?
--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-dev/29f714ee-c04d-4e78-ae1e-a466cf7fde24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...