- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $sortArray (aggregation)
$sortArray (aggregation)¶
On this page
Definition¶
-
$sortArray¶ New in version 5.2.
Sorts an array based on its elements. The sort order is user specified.
$sortArrayhas the following syntax:Field Type Description inputexpression The array to be sorted.
The result is
nullif the expression:- is missing
- evaluates to
null - evaluates to
undefined
If the expression evaluates to any other non-array value, the document returns an error.
sortBydocument The document specifies a sort ordering.
Behavior¶
The $sortArray expression orders the input array
according to the sortBy specification.
The $sortArray syntax and semantics are the same as the
behavior in a $push operation modified by $sort.
Sort by Document Fields¶
If the array elements are documents, you can sort by a document field.
Specify the field name and a sort direction, ascending (1), or
descending (-1 ).
Sort by Value¶
To sort the whole array by value, or to sort by array elements that are
not documents, identify the input array and specify 1 for an ascending
sort or -1 for descending sort in the sortBy parameter.
Considerations¶
- There is no implicit array traversal on the sort key.
- Positional operators are not supported. A field name like “values.1”
denotes a sub-field called “1” in the
valuesarray. It does not refer to the item at index 1 in thevaluesarray. - When a whole array is sorted, the sort is lexicographic. The
aggregation
$sortstage, behaves differently. See$sortfor more details. - When an array is sorted by a field, any documents or scalars that do not have the specified field are sorted equally. The resulting sort order is undefined.
nullvalues and missing values sort equally.
Sort Stability¶
The stability of the sort is not specified. Users should not rely on
$sortArray to use a particular sorting algorithm.
Example¶
Create the team collection:
The team array has three elements. Each element of team has
nested sub-elements: name, age, and address. The following
examples show how to sort the team array using these sub-elements.
Sort on a Field¶
Sort on a field within an array element:
The name field is a sub-element in the team array. The
operation returns the following results:
Sort on a Subfield¶
The address field is a document with subfields of its own. Use dot
notation to sort the array based on a subfield:
The sort direction is descending because the sortBy value is “-1”.
Sort an Array of Integers¶
This example specifies an input array directly. The values are all the same type, Int32:
Example output:
The input array has a “1” in position 0 and position 2. The ones are grouped together in the results, but their are no guarantees regarding how the group of ones is sorted relative to their original order.
Sort on Mixed Type Fields¶
This example specifies an input array directly. The values have different types:
Example output:
The results are ordered.
In contrast, after changing the sortBy field to sort on the one of
the document fields, sortBy: { a: 1 }, the sort order for the
scalar and null values is undefined: