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>:
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 }不正确)。Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。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. $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可用的修饰符列表,请参阅修饰符。
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 }
]
}
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 以下操作向scores array and sorts the elements:scores数组添加两个元素并对元素进行排序:
db.students.updateOne(
{ _id: 2 },
{ $push: { tests: { $each: [ 40, 60 ], $sort: 1 } } }
)
The updated document has the elements of the 更新后的文档具有按升序排列的scores array in ascending order:scores数组元素:
{ "_id" : 2, "tests" : [ 40, 50, 60, 70, 89, 89 ] }
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, as in the following:tests字段以按降序对其元素排序,请指定{ $sort: -1 },并为$each饰符指定空数组[],如下所示:
db.students.updateOne(
{ _id: 3 },
{ $push: { tests: { $each: [ ], $sort: -1 } } }
)
The result of the operation is to update the 该操作的结果是更新scores field to sort its elements in descending order:scores字段,以按降序对其元素进行排序:
{ "_id" : 3, "tests" : [ 100, 89, 70, 20 ] }
$sort with Other $push Modifiers$sort与其他$push修饰符一起使用Add 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操作使用:
$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数组的前三个排序元素。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 is immaterial to the order in which the modifiers are processed. 修饰符的顺序与修饰符的处理顺序无关。See Modifiers for details.有关详细信息,请参阅修饰符。