- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $sort
$sort¶
-
$sort¶ New in version 2.4.
The
$sortmodifier orders the elements of an array during a$pushoperation. The elements of the array must be documents.$sortmodifies$pushupdates that use both the$eachand$slicemodifiers, where$eachis the first modifier for the$pushoperation.Important
The
<sort document>only accesses the fields from the elements in the array and does not refer to the array<field>.Consider the following example where the collection
studentscontain the following document:The following update appends additional documents to the
quizzesarray, sorts all the elements of the array by ascendingscorefield, and slices the array to keep the last five elements:After the update, the array elements are in order of ascending
scorefield.: