Skip to content

Commit fd6bf56

Browse files
mdb-ashleyAshley Brown
andauthored
Add sort limit to $sort (aggregation) and cursor.sort() (#801) (#807)
* Adding sort limit in a few places * Resolving comment Co-authored-by: Ashley Brown <[email protected]> Co-authored-by: Ashley Brown <[email protected]>
1 parent f3c7de0 commit fd6bf56

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

source/includes/sort-limits.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can sort on a maximum of 32 keys.

source/reference/limits.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ Indexes
215215

216216
.. include:: /includes/extracts/collation-index-type-restrictions-addendum.rst
217217

218+
.. limit:: Hidden Indexes
219+
220+
- You cannot :doc:`hide </core/index-hidden>` the ``_id`` index.
221+
222+
- You cannot use :method:`~cursor.hint()` on a :doc:`hidden index
223+
</core/index-hidden>`.
224+
225+
Sorts
226+
-----
227+
228+
.. limit:: Maximum Number of Sort Keys
229+
230+
.. include:: /includes/sort-limits.rst
231+
218232
Data
219233
----
220234

source/reference/method/cursor.sort.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ Definition
5757
existing fields <sort-asc-desc>` or :ref:`sort on computed metadata
5858
<sort-metadata>`.
5959

60-
Behaviors
61-
---------
60+
Behavior
61+
--------
62+
63+
Limits
64+
~~~~~~
65+
66+
.. include:: /includes/sort-limits.rst
6267

6368
Result Ordering
6469
~~~~~~~~~~~~~~~

source/reference/operator/aggregation/sort.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,88 @@ Definition
5252
``<field1>``. Then documents with the same ``<field1>`` values are
5353
further sorted by ``<field2>``.
5454

55+
Behavior
56+
--------
57+
58+
Limits
59+
~~~~~~
60+
61+
.. include:: /includes/sort-limits.rst
62+
63+
.. _sort-aggregation-consistent-sorting:
64+
65+
Sort Consistency
66+
~~~~~~~~~~~~~~~~
67+
68+
.. include:: /includes/fact-sort-consistency.rst
69+
70+
Consider the following ``restaurant`` collection:
71+
72+
.. code-block:: js
73+
74+
db.restaurants.insertMany( [
75+
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
76+
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
77+
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
78+
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
79+
{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
80+
] );
81+
82+
The following command uses the :pipeline:`$sort` stage to sort on
83+
the ``borough`` field:
84+
85+
.. code-block:: js
86+
87+
db.restaurants.aggregate(
88+
[
89+
{ $sort : { borough : 1 } }
90+
]
91+
)
92+
93+
In this example, sort order may be inconsistent, since the ``borough``
94+
field contains duplicate values for both ``Manhattan`` and ``Brooklyn``.
95+
Documents are returned in alphabetical order by ``borough``, but the
96+
order of those documents with duplicate values for ``borough`` might not
97+
the be the same across multiple executions of the same sort. For
98+
example, here are the results from two different executions of the
99+
above command:
100+
101+
.. code-block:: js
102+
:copyable: false
103+
104+
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
105+
{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
106+
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
107+
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
108+
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
109+
110+
{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
111+
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
112+
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
113+
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
114+
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
115+
116+
While the values for ``borough`` are still sorted in alphabetical order,
117+
the order of the documents containing duplicate values for ``borough``
118+
(i.e. ``Manhattan`` and ``Brooklyn``) is not the same.
119+
120+
To achieve a *consistent sort*, add a field which contains exclusively
121+
unique values to the sort. The following command uses the
122+
:pipeline:`$sort` stage to sort on both the ``borough`` field and the
123+
``_id`` field:
124+
125+
.. code-block:: js
126+
127+
db.restaurants.aggregate(
128+
[
129+
{ $sort : { borough : 1, _id: 1 } }
130+
]
131+
)
132+
133+
Since the ``_id`` field is always guaranteed to contain exclusively
134+
unique values, the returned sort order will always be the same across
135+
multiple executions of the same sort.
136+
55137
Examples
56138
--------
57139

0 commit comments

Comments
 (0)