Skip to content

Commit dfae0b7

Browse files
jeff-allen-mongokay-kim
authored andcommitted
DOCS-11241 - Updating compatibility notes for sorting on multiple array fields
1 parent c7b45db commit dfae0b7

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

source/release-notes/3.6-compatibility.txt

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Compatibility Changes in MongoDB 3.6
1212

1313
The following 3.6 changes can affect the compatibility with older
1414
versions of MongoDB.
15-
15+
1616
.. _3.6-bind_ip-compatibility:
1717

1818
Localhost Binding Compatibility Changes
@@ -233,6 +233,75 @@ For more information on sorting with the
233233
:doc:`Aggregation Pipeline </reference/operator/aggregation/>`, see
234234
:doc:`$sort </reference/operator/aggregation/sort/>`.
235235

236+
Sorting with a Compound Sort Pattern on Multiple Array Fields with ``aggregate``
237+
````````````````````````````````````````````````````````````````````````````````
238+
239+
In MongoDB 3.6, when sorting in an
240+
:doc:`aggregation pipeline </core/aggregation-pipeline/>`, MongoDB can
241+
no longer sort with keys that are *parallel arrays*. Arrays are
242+
considered parallel if they are sibling elements of the BSON object.
243+
Sort keys involving nested arrays are not considered parallel, nor are
244+
sort keys which share the same array as a prefix.
245+
246+
.. note::
247+
248+
This behavior has always existed for sorting with
249+
:method:`find <db.collection.find()>`, but now in 3.6
250+
:method:`find <db.collection.find()>` and
251+
:method:`aggregate <db.collection.aggregate()>` share the same
252+
semantics.
253+
254+
.. example::
255+
256+
A collection contains the following document:
257+
258+
.. code-block:: javascript
259+
260+
{a: [ {b: [1, 2]}, {b: [3, 4]} ]}
261+
262+
The following aggregation succeeds because the sort is performed on
263+
a nested array:
264+
265+
.. code-block:: javascript
266+
267+
db.coll.aggregate([{$sort: {"a.b": 1}}])
268+
269+
.. example::
270+
271+
Similarly, if a collection contains the following document:
272+
273+
.. code-block:: javascript
274+
275+
{a: [{b: 1, c: 1}, {b: 2, c: 2}]}
276+
277+
The following aggregation succeeds because the sort keys share the
278+
same array as a prefix:
279+
280+
.. code-block:: javascript
281+
282+
db.coll.aggregate([{$sort: {"a.b": 1, "a.c": 1}}])
283+
284+
.. example::
285+
286+
However, in a collection that contains the following documents:
287+
288+
.. code-block:: javascript
289+
290+
{ _id: 1, a: [ 1, 2 ], b: [ 1, 2 ]}
291+
{ _id: 2, a: [ -3, 5 ], b: 0 }
292+
{ _id: 3, a: [ -6, 12 ], b: 100 }
293+
294+
The following sort operation fails:
295+
296+
.. code-block:: javascript
297+
298+
db.coll.aggregate([ { $sort: {a: 1, b: 1} } ])
299+
300+
MongoDB cannot sort on both the ``a`` and ``b`` fields because
301+
in the document with ``_id : 1`` , both the ``a`` and ``b`` fields
302+
are arrays. As a result, MongoDB encounters a parallel array during
303+
sort key generation and returns an error.
304+
236305
Update Operation Changes
237306
------------------------
238307

0 commit comments

Comments
 (0)