$sort
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
修饰符才有效果。{
$push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$sort: <sort specification>
}
}
}For用于<sort specification>
:<sort specification>
:To sort array elements that are not documents, or if the array elements are documents, to sort by the whole documents, specify若要对非文档的数组元素进行排序,或者如果数组元素是文档,则按整个文档进行排序,请指定1
for ascending or-1
for descending.1
表示升序,或指定-1
表示降序。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 }
.{ field: 1 }
或{ field: -1 }
。Do not reference the containing array field in the sort specification (e.g.不要引用排序规范中包含的数组字段(例如{ "arrayField.field": 1 }
is incorrect).{ "arrayField.field": 1 }
不正确)。
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. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
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
可用的修饰符列表,请参阅修饰符。
Examples实例
Sort Array of Documents by a Field in the Documents按文档中的字段对文档数组进行排序
Create the 创建students
collection:students
集合:
db.students.insertOne(
{
"_id": 1,
"quizzes": [
{ "id" : 1, "score" : 6 },
{ "id" : 2, "score" : 9 }
]
}
)
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
字段对数组中的所有元素进行排序:
db.students.updateOne(
{ _id: 1 },
{
$push: {
quizzes: {
$each: [ { id: 3, score: 8 }, { id: 4, score: 7 }, { id: 5, score: 6 } ],
$sort: { score: 1 }
}
}
}
)
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
字段升序排列:
{
"_id" : 1,
"quizzes" : [
{ "id" : 1, "score" : 6 },
{ "id" : 5, "score" : 6 },
{ "id" : 4, "score" : 7 },
{ "id" : 3, "score" : 8 },
{ "id" : 2, "score" : 9 }
]
}
Sort Array Elements That Are Not Documents对非文档的数组元素进行排序
Add the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne( { "_id" : 2, "tests" : [ 89, 70, 89, 50 ] } )
The following operation adds two more elements to the 以下操作将另外两个元素添加到tests
array and sorts the elements:tests
数组并对元素进行排序:
db.students.updateOne(
{ _id: 2 },
{ $push: { tests: { $each: [ 40, 60 ], $sort: 1 } } }
)
The updated document has the elements of the 更新后的文档中tests
array in ascending order:tests
数组的元素按升序排列:
{ "_id" : 2, "tests" : [ 40, 50, 60, 70, 89, 89 ] }
Update Array Using Sort Only仅使用排序更新数组
Add the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne( { "_id" : 3, "tests" : [ 89, 70, 100, 20 ] } )
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. tests
字段以按降序对其元素进行排序,请指定{ $sort: -1 }
,并为$each
修饰符指定一个空数组[]
。For example:例如:
db.students.updateOne(
{ _id: 3 },
{ $push: { tests: { $each: [ ], $sort: -1 } } }
)
The example sorts the 此示例按降序对tests
field values in descending order:tests
字段值进行排序:
{ "_id" : 3, "tests" : [ 100, 89, 70, 20 ] }
Use $sort
with Other $push
Modifiers将$sort
与其他$push
修饰符一起使用
$sort
with Other $push
ModifiersAdd the following document to the 将以下文档添加到students
collection:students
集合中:
db.students.insertOne(
{
"_id" : 5,
"quizzes" : [
{ "wk": 1, "score" : 10 },
{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },
{ "wk": 4, "score" : 6 }
]
}
)
The following 以下$push
operation uses:$push
操作使用:
the$each
modifier to add multiple documents to thequizzes
array,$each
修饰符将多个文档添加到测验数组,the$sort
modifier to sort all the elements of the modifiedquizzes
array by thescore
field in descending order, and$sort
修饰符,用于按score
字段降序对修改后的quizzes
数组的所有元素进行排序,以及the$slice
modifier to keep only the first three sorted elements of thequizzes
array.$slice
修饰符只保留quizzes
数组的前三个排序元素。
db.students.updateOne(
{ _id: 5 },
{
$push: {
quizzes: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3
}
}
}
)
After the operation only the three highest scoring quizzes are in the array:手术后,数组中只有三个得分最高的测验:
{
"_id" : 5,
"quizzes" : [
{ "wk" : 1, "score" : 10 },
{ "wk" : 2, "score" : 8 },
{ "wk" : 5, "score" : 8 }
]
}
The order of the modifiers in the query does not change the order that the modifiers are applied. For details, see Modifiers.查询中修饰符的顺序不会更改应用修饰符的顺序。有关详细信息,请参阅修饰符。