Skip to content

Commit 30c263b

Browse files
drmirrorSam Kleinman
authored andcommitted
Explain how mongos handles query options other than sort()
Gathered from a discussion with Richard and some code-reading. Signed-off-by: Sam Kleinman <[email protected]>
1 parent 334ba75 commit 30c263b

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

source/core/sharded-cluster-query-router.txt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,29 @@ collection.
7878
{ zipcode: 1, u_id: 1 }
7979
{ zipcode: 1, u_id: 1, c_date: 1 }
8080

81-
How ``mongos`` Establishes a Cursor on Targeted Shards
82-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81+
How ``mongos`` Handles Query Flags
82+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83+
84+
If the result of the query is unsorted, the :program:`mongos` instance opens
85+
a result cursor that "round robins" results from all cursors on the
86+
shards.
8387

84-
When the first batch of results returns from the cursors, the
85-
:program:`mongos` instance does one of the following:
88+
.. versionChanged::2.0.5
89+
In versions prior to 2.0.5, the :program:`mongos` exhausted each cursor, one by one.
8690

87-
- For query with sorted results (i.e. using :method:`cursor.sort()`) the
88-
:program:`mongos` instance performs a merge sort of all queries.
91+
If the result is supposed to be sorted (i.e. the query used
92+
:method:`cursor.sort()`), the :program:`mongos` instance passes the `sort()` flag down to
93+
the targeted shards, and performs a merge sort of the results.
8994

90-
- For a query with unsorted results, the :program:`mongos` instance
91-
returns a result cursor that "round robins" results from all cursors
92-
on the shards.
95+
If the query used :method:`cursor.limit()`, the :program:`mongos` instance passes that limit
96+
down to the shards and then re-applies it to the complete result.
9397

94-
.. versionchanged:: 2.0.5
95-
Before 2.0.5, the :program:`mongos` exhausted each cursor,
96-
one by one.
98+
If the query used :method:`cursor.skip()`, the :program:`mongos` instance cannot pass it
99+
down to the shards since it cannot know in advance how much of the
100+
skip will be filled by each individual result. The :program:`mongos` instance
101+
therefore does *not* pass the `skip()` value down, but instead passes a
102+
`limit()` value of `(skip+limit)` down to the shards, and then performs
103+
the actual skip when assembling the complete result.
97104

98105
Detect Connections to :program:`mongos` Instances
99106
-------------------------------------------------

0 commit comments

Comments
 (0)