$slice

  • $slice
  • The $slice modifier limits the number of arrayelements during a $push operation. To project, or return,a specified number of array elements from a read operation, see the$slice projection operator instead.

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

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

The <num> can be:

ValueDescriptionZeroTo update the array <field> to an empty array.NegativeTo update the array <field> to contain only the last<num> elements.PositiveTo update the array <field> contain only the first <num>elements.

New in version 2.6.

Behavior

The order in which the modifiers appear is immaterial. Previousversions required the $each modifier to appear as the firstmodifier if used in conjunction with $slice. For a list ofmodifiers available for $push, see Modifiers.

Trying to use the $slice modifier without the $eachmodifier results in an error.

Examples

Slice from the End of the Array

A collection students contains the following document:

  1. { "_id" : 1, "scores" : [ 40, 50, 60 ] }

The following operation adds new elements to the scores array, andthen uses the $slice modifier to trim the array to thelast five elements:

  1. db.students.update(
  2. { _id: 1 },
  3. {
  4. $push: {
  5. scores: {
  6. $each: [ 80, 78, 86 ],
  7. $slice: -5
  8. }
  9. }
  10. }
  11. )

The result of the operation is slice the elements of the updatedscores array to the last five elements:

  1. { "_id" : 1, "scores" : [ 50, 60, 80, 78, 86 ] }

Slice from the Front of the Array

A collection students contains the following document:

  1. { "_id" : 2, "scores" : [ 89, 90 ] }

The following operation adds new elements to the scores array, andthen uses the $slice modifier to trim the array to thefirst three elements.

  1. db.students.update(
  2. { _id: 2 },
  3. {
  4. $push: {
  5. scores: {
  6. $each: [ 100, 20 ],
  7. $slice: 3
  8. }
  9. }
  10. }
  11. )

The result of the operation is to slice the elements of the updatedscores array to the first three elements:

  1. { "_id" : 2, "scores" : [ 89, 90, 100 ] }

Update Array Using Slice Only

A collection students contains the following document:

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

To update the scores field with just the effects of the$slice modifier, specify the number of elements to slice(e.g. -3) for the $slice modifier and an emptyarray [] for the $each modifier, as in the following:

  1. db.students.update(
  2. { _id: 3 },
  3. {
  4. $push: {
  5. scores: {
  6. $each: [ ],
  7. $slice: -3
  8. }
  9. }
  10. }
  11. )

The result of the operation is to slice the elements of the scoresarray to the last three elements:

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

Use $slice 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.