- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $sort
$sort¶
-
$sort¶ The
$sortmodifier orders the elements of an array during a$pushoperation.To use the
$sortmodifier, it must appear with the$eachmodifier. You can pass an empty array[]to the$eachmodifier such that only the$sortmodifier has an effect.For
<sort specification>:- To sort array elements that are not documents, or if the array
elements are documents, to sort by the whole documents, specify
1for ascending or-1for descending. - If the array elements are documents, to sort by a field in the
documents, specify a sort document with the field and the
direction, i.e.
{ field: 1 }or{ field: -1 }. Do not reference the containing array field in the sort specification (e.g.{ "arrayField.field": 1 }is incorrect).
- To sort array elements that are not documents, or if the array
elements are documents, to sort by the whole documents, specify
Behavior¶
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.
The $sort modifier can sort array elements that are not
documents. In previous versions, the $sort modifier required
the array elements be documents.
If the array elements are documents, the modifier can sort by either
the whole document or by a specific field in the documents. In previous
versions, the $sort modifier can only sort by a specific
field in the documents.
Trying to use the $sort modifier without the $each
modifier results in an error. The $sort no longer requires
the $slice modifier. For a list of modifiers available for
$push, see Modifiers.
Examples¶
Sort Array of Documents by a Field in the Documents¶
Create the students collection:
The following update appends additional documents to the quizzes
array and then sorts all the elements of the array by the ascending
score field:
Important
The sort document refers directly to the field in the
documents and does not reference the containing array field
quizzes; i.e. { score: 1 } and not { "quizzes.score": 1}
After the update, the array elements are in order of ascending
score field.:
Sort Array Elements That Are Not Documents¶
Add the following document to the students collection:
The following operation adds two more elements to the scores array
and sorts the elements:
The updated document has the elements of the scores array in
ascending order:
Update Array Using Sort Only¶
Add the following document to the students collection:
To update the tests field to sort its elements in descending
order, specify the { $sort: -1 } and specify an empty array []
for the $each modifier, as in the following:
The result of the operation is to update the scores field to sort
its elements in descending order:
Use $sort with Other $push Modifiers¶
Add the following document to the students collection:
The following $push operation uses:
- the
$eachmodifier to add multiple documents to thequizzesarray, - the
$sortmodifier to sort all the elements of the modifiedquizzesarray by thescorefield in descending order, and - the
$slicemodifier to keep only the first three sorted elements of thequizzesarray.
After the operation only the three highest scoring quizzes are in the array:
The order of the modifiers is immaterial to the order in which the modifiers are processed. See Modifiers for details.