@@ -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+
55137Examples
56138--------
57139
0 commit comments