Docs HomeMongoDB Manual

$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 }
}
}
}
)
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字段升序排列:

{
"_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修饰符一起使用

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操作使用:

  • the $each modifier to add multiple documents to the quizzes array,$each修饰符将多个文档添加到测验数组,
  • the $sort modifier to sort all the elements of the modified quizzes array by the score field in descending order, and$sort修饰符,用于按score字段降序对修改后的quizzes数组的所有元素进行排序,以及
  • the $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 in the query does not change the order that the modifiers are applied. For details, see Modifiers.查询中修饰符的顺序不会更改应用修饰符的顺序。有关详细信息,请参阅修饰符