On this page本页内容
$sort
¶The $sort
modifier orders the elements of an array during a $push
operation.$sort
修饰符在$push
操作期间对数组的元素进行排序。
To use the 要使用$sort
modifier, it must appear with the $each
modifier. $sort
修饰符,它必须与$each
修饰符一起出现。You can pass an empty array 可以将空数组[]
to the $each
modifier such that only the $sort
modifier has an effect.[]
传递给$each
修饰符,这样只有$sort
修饰符有效。
For 对于<sort specification>
:<sort specification>
:
1
for ascending or -1
for descending.1
表示升序,指定-1
表示降序。{ field: 1 }
or { field: -1 }
. {field:1}
或{field:-1}
。{ "arrayField.field": 1 }
is incorrect).{ "arrayField.field": 1 }
不正确)。The $sort
modifier can sort array elements that are not documents. $sort
修饰符可以对非文档的数组元素进行排序。In previous versions, the 在以前的版本中,$sort
modifier required the array elements be documents.$sort
修饰符要求数组元素是文档。
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.$sort
修饰符只能按文档中的特定字段进行排序。
Trying to use the 尝试在没有$sort
modifier without the $each
modifier results in an error. $each
修饰符的情况下使用$sort
修饰符会导致错误。The $sort
no longer requires the $slice
modifier. $sort
不再需要$slice
修饰符。For a list of modifiers available for 有关$push
, see Modifiers.$push
可用的修饰符列表,请参阅修饰符。
A collection students
contains the following document:students
集合包括以下文档:
The following update appends additional documents to the 以下更新会将其他文档附加到quizzes
array and then sorts all the elements of the array by the ascending score
field:quizzes
数组,然后按递增score
字段对数组中的所有元素进行排序:
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}
quizzes
;亦即{score:1}
而不是{ "quizzes.score": 1}
。
After the update, the array elements are in order of ascending 更新后,数组元素按score
field.:score
字段升序排列:
A collection students
contains the following document:students
集合包括以下文件:
The following operation adds two more elements to the 以下操作向scores
array and sorts the elements:scores
数组中再添加两个元素,并对元素进行排序:
The updated document has the elements of the 更新后的文档按升序包含scores
array in ascending order:scores
数组的元素:
A collection students
contains the following document:students
集合包括以下文档:
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:tests
字段以按降序对其元素进行排序,请指定{ $sort: -1 }
,并为$each
修饰符指定一个空数组[]
,如下所示:
The result of the operation is to update the 操作的结果是更新scores
field to sort its elements in descending order:scores
字段,以按降序对其元素进行排序:
$sort
with Other $push
Modifiers$sort
与其他$push
修饰符一起使用¶A collection students
has the following document:students
集合有以下文档:
The following 以下$push
operation uses:$push
操作使用:
$each
modifier to add multiple documents to the quizzes
array,$each
修饰符将多个文档添加到quizzes
数组中,$sort
modifier to sort all the elements of the modified quizzes
array by the score
field in descending order, and$sort
修饰符,用于按score
字段降序排列修改后的quizzes
数组的所有元素,以及$slice
modifier to keep only the first three sorted elements of the quizzes
array.$slice
修饰符只保留quizzes
数组的前三个排序元素。The result of the operation is keep only the three highest scoring quizzes:该操作的结果是只保留三个得分最高的测验:
The order of the modifiers is immaterial to the order in which the modifiers are processed. 修饰语的顺序与修饰语的处理顺序无关。See Modifiers for details.有关详细信息,请参阅修饰符。