The actual query was executed this way in your cluster with two shards:
- Two cursors were opened by
mongos
router against the two shards.
- The query predicate
{ index : { $gt : 4990, $lte : 5000 } }
was executed on two shards (and returned the cursors to the mongos
); mongos
has two results.
-
mongos
merged the two results.
-
mongos
applied the skip on the merged result.
NOTE: The mongos
applies the skip to the merged set of results (not at the shard level).
Based upon the above steps, the query returns the five documents - as expected.
You have generated a query plan with “executionStats”. And, your question is more related to the query plan numbers from the “executionStats”. I will try to explain.
Stages:
- SHARD_MERGE: The stage is for merging results from shards.
- SHARDING_FILTER: The step specifies the documents fetched from a particular shard. The
mongos
compares the shard key of the document with the metadata on the config servers to get the documents.
These are some details from the plan’s “executionStats”:
"executionStages" : {
"stage" : "SHARD_MERGE",
"nReturned" : 2,
"shards" : [
"shardName" : "shard01"
"stage" : "SKIP",
"nReturned" : 0,
"stage" : "SHARDING_FILTER"
"nReturned" : 3
"shardName" : "shard02"
"stage" : "SKIP",
"nReturned" : 2,
"stage" : "SHARDING_FILTER"
"nReturned" : 7
]
}
The "nReturned"
values from the "stage" : "SHARDING_FILTER"
is the actual number of documents returned from the cursors (from each shard). Note the values of "nReturned"
are 3 and 7 (sums to 10); the total number of documents returned to the mongos
. The skip(5)
is applied to this total number of 10 documents, returning 5 documents (the actual result).
If you notice, each of the shards also have a "stage" : "SKIP".
And, the "nReturned"
value for that shard is actually the skip applied on the "nReturned"
value from the "stage" : "SHARDING_FILTER"
. For example, for the "shard02"
, the skip is applied on the value 7 and which results as 2. For the "shard01"
, the skip is applied on 3, which results a negative number and hence shows 0. But, actually the skip is never applied - these are just projections on each shard, I think. The sum of these projections are shown in the "stage" : "SHARD_MERGE", "nReturned" : 2
.