Skip to content

Commit c24b256

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCS-15307 sort order arrays (#1107) (#1143)
* DOCS-15307-sort-order-arrays * DOCS-15307-sort-order-arrays * DOCS-15307-sort-order-arrays * DOCS-15307-sort-order-arrays * DOCS-15307-sort-order-arrays Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent d0b4131 commit c24b256

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

source/includes/sorting-arrays.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
In array comparisons:
2+
3+
- A less-than comparison or descending sort compares the smallest
4+
elements of the array according to the BSON type sort order.
5+
6+
- A greater-than comparison or descending sort compares the largest
7+
elements of the array according to the reverse BSON type sort order.
8+
9+
- When comparing a field whose value is a one element array (example,
10+
``[ 1 ]``) with non-array fields (example, ``2``), the comparison is
11+
for ``1`` and ``2``.
12+
13+
- A comparison of an empty array (example, ``[ ]``) considers the empty
14+
array as less than a ``null`` value or a missing field value.

source/reference/bson-type-comparison-order.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,12 @@ Collation specification has the following syntax:
4444

4545
.. include:: /includes/extracts/collation-unspecified.rst
4646

47+
.. _bson-type-comparison-order-arrays:
48+
4749
Arrays
4850
------
4951

50-
With arrays, a less-than comparison or an ascending sort compares the
51-
smallest element of arrays, and a greater-than comparison or a
52-
descending sort compares the largest element of the arrays. As such,
53-
when comparing a field whose value is a single-element array (e.g. ``[
54-
1 ]``) with non-array fields (e.g. ``2``), the comparison is between
55-
``1`` and ``2``. A comparison of an empty array (e.g. ``[ ]``) treats
56-
the empty array as less than ``null`` or a missing field.
52+
.. include:: /includes/sorting-arrays.rst
5753

5854
Objects
5955
-------

source/tutorial/sort-results-with-indexes.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ key field.
224224
- When an index has a key with multiple data types, the index is
225225
sorted according to the :ref:`BSON type sort order
226226
<bson-types-comparison-order>`.
227-
- If an index key contains an array, the document is sorted according
228-
to the type of the first array element.
227+
228+
- .. include:: /includes/sorting-arrays.rst
229229

230230
See the :ref:`index sorting example <ex-sort-index-types>`.
231231

@@ -377,8 +377,7 @@ The documents are returned in :ref:`BSON type sort order
377377
type: 'Timestamp'
378378
}
379379

380-
- Arrays are sorted according to the type of the first element. In this
381-
example the first element types are: Int32, String, and Array.
380+
- .. include:: /includes/sorting-arrays.rst
382381
- Numerical types (Int32, Long, Decimal128, Double) are equivalent when
383382
compared with other types.
384383
- Within the Numbers BSON type, numerical types are sorted:

0 commit comments

Comments
 (0)