$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行为

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按文档中的字段对文档数组进行排序

A collection students contains the following document:students集合包括以下文档:

{
  "_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.update(
   { _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对非文档的数组元素进行排序

A collection students contains the following document:students集合包括以下文件:

{ "_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.update(
   { _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 ] }

Update Array Using Sort Only仅使用排序更新数组

A collection students contains the following document:students集合包括以下文档:

{ "_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.update(
   { _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 ] }

Use $sort with Other $push Modifiers$sort与其他$push修饰符一起使用

A collection students has the following document:students集合有以下文档:

{
   "_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修饰符将多个文档添加到quizzes数组中,
  • 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.update(
   { _id: 5 },
   {
     $push: {
       quizzes: {
          $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
          $sort: { score: -1 },
          $slice: 3
       }
     }
   }
)

The result of the operation is keep only the three highest scoring quizzes:该操作的结果是只保留三个得分最高的测验:

{
  "_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.有关详细信息,请参阅修饰符