$sort

  • $sort
  • The $sort modifier orders the elements of an arrayduring a $push operation.

To use the $sort modifier, it must appear with the$each modifier. You can pass an empty array [] to the$each modifier such that only the $sort modifierhas an effect.

  1. {
  2. $push: {
  3. <field>: {
  4. $each: [ <value1>, <value2>, ... ],
  5. $sort: <sort specification>
  6. }
  7. }
  8. }

For <sort specification>:

  • To sort array elements that are not documents, or if the arrayelements are documents, to sort by the whole documents, specify1 for ascending or -1 for descending.
  • If the array elements are documents, to sort by a field in thedocuments, specify a sort document with the field and thedirection, i.e. { field: 1 } or { field: -1 }. Do notreference the containing array field in the sort specification(e.g. { "arrayField.field": 1 } is incorrect).

Behavior

The $sort modifier can sort array elements that are notdocuments. In previous versions, the $sort modifier requiredthe array elements be documents.

If the array elements are documents, the modifier can sort by eitherthe whole document or by a specific field in the documents. In previousversions, the $sort modifier can only sort by a specificfield in the documents.

Trying to use the $sort modifier without the $eachmodifier results in an error. The $sort no longer requiresthe $slice modifier. For a list of modifiers available for$push, see Modifiers.

Examples

Sort Array of Documents by a Field in the Documents

A collection students contains the following document:

  1. {
  2. "_id": 1,
  3. "quizzes": [
  4. { "id" : 1, "score" : 6 },
  5. { "id" : 2, "score" : 9 }
  6. ]
  7. }

The following update appends additional documents to the quizzesarray and then sorts all the elements of the array by the ascendingscore field:

  1. db.students.update(
  2. { _id: 1 },
  3. {
  4. $push: {
  5. quizzes: {
  6. $each: [ { id: 3, score: 8 }, { id: 4, score: 7 }, { id: 5, score: 6 } ],
  7. $sort: { score: 1 }
  8. }
  9. }
  10. }
  11. )

Important

The sort document refers directly to the field in thedocuments and does not reference the containing array fieldquizzes; i.e. { score: 1 } and not { "quizzes.score": 1}

After the update, the array elements are in order of ascendingscore field.:

  1. {
  2. "_id" : 1,
  3. "quizzes" : [
  4. { "id" : 1, "score" : 6 },
  5. { "id" : 5, "score" : 6 },
  6. { "id" : 4, "score" : 7 },
  7. { "id" : 3, "score" : 8 },
  8. { "id" : 2, "score" : 9 }
  9. ]
  10. }

Sort Array Elements That Are Not Documents

A collection students contains the following document:

  1. { "_id" : 2, "tests" : [ 89, 70, 89, 50 ] }

The following operation adds two more elements to the scores arrayand sorts the elements:

  1. db.students.update(
  2. { _id: 2 },
  3. { $push: { tests: { $each: [ 40, 60 ], $sort: 1 } } }
  4. )

The updated document has the elements of the scores array inascending order:

  1. { "_id" : 2, "tests" : [ 40, 50, 60, 70, 89, 89 ] }

Update Array Using Sort Only

A collection students contains the following document:

  1. { "_id" : 3, "tests" : [ 89, 70, 100, 20 ] }

To update the tests field to sort its elements in descendingorder, specify the { $sort: -1 } and specify an empty array []for the $each modifier, as in the following:

  1. db.students.update(
  2. { _id: 3 },
  3. { $push: { tests: { $each: [ ], $sort: -1 } } }
  4. )

The result of the operation is to update the scores field to sortits elements in descending order:

  1. { "_id" : 3, "tests" : [ 100, 89, 70, 20 ] }

Use $sort with Other $push Modifiers

A collection students has the following document:

  1. {
  2. "_id" : 5,
  3. "quizzes" : [
  4. { "wk": 1, "score" : 10 },
  5. { "wk": 2, "score" : 8 },
  6. { "wk": 3, "score" : 5 },
  7. { "wk": 4, "score" : 6 }
  8. ]
  9. }

The following $push operation uses:

  • the $each modifier to add multiple documents to thequizzes array,
  • the $sort modifier to sort all the elements of themodified quizzes array by the score field in descendingorder, and
  • the $slice modifier to keep only the first threesorted elements of the quizzes array.
  1. db.students.update(
  2. { _id: 5 },
  3. {
  4. $push: {
  5. quizzes: {
  6. $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
  7. $sort: { score: -1 },
  8. $slice: 3
  9. }
  10. }
  11. }
  12. )

The result of the operation is keep only the three highest scoring quizzes:

  1. {
  2. "_id" : 5,
  3. "quizzes" : [
  4. { "wk" : 1, "score" : 10 },
  5. { "wk" : 2, "score" : 8 },
  6. { "wk" : 5, "score" : 8 }
  7. ]
  8. }

The order of the modifiers is immaterial to the order in which themodifiers are processed. See Modifiers for details.