Skip to content

Commit 6654c93

Browse files
committed
DOCSP-9558 added note about stable sorts
1 parent d2fdb0a commit 6654c93

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

source/fundamentals/crud/read/limit.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,29 @@ longest books:
110110
You can combine ``skip()`` and ``limit()`` in this way to implement paging for your
111111
collection, returning only small subsets of the collection at one time.
112112

113+
.. note::
114+
115+
In order to ensure stable sorts across multiple queries, you must sort
116+
using a unique key (such as ``_id``). Otherwise, a call to ``skip()``
117+
and ``limit()`` may produce unpredictable results when combined with
118+
``sort()``.
119+
120+
For example, consider the following data:
121+
122+
.. code_block:: java
123+
124+
{ type: "computer", data: "1", serial_no: 235235 }
125+
{ type: "computer", data: "2", serial_no: 235237 }
126+
{ type: "computer", data: "3", serial_no: 235239 }
127+
{ type: "computer", data: "4", serial_no: 235241 }
128+
129+
If you sorted by ``type`` alone, ``sort()`` does not guarantee the same order
130+
upon return. Appending ``skip()`` and ``limit()`` to the ``sort()``
131+
could return different documents for different queries. In this case, sorting
132+
by ``data`` or ``serial_no`` would guarantee a stable sort, as both are unique keys.
133+
134+
135+
113136
For additional information on the classes and methods mentioned on this
114137
page, see the following API documentation:
115138

0 commit comments

Comments
 (0)