$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 $slice
modifier has an effect.
- {
- $push: {
- <field>: {
- $each: [ <value1>, <value2>, ... ],
- $slice: <num>
- }
- }
- }
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 $each
modifier results in an error.
Examples
Slice from the End of the Array
A collection students
contains the following document:
- { "_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:
- db.students.update(
- { _id: 1 },
- {
- $push: {
- scores: {
- $each: [ 80, 78, 86 ],
- $slice: -5
- }
- }
- }
- )
The result of the operation is slice the elements of the updatedscores
array to the last five elements:
- { "_id" : 1, "scores" : [ 50, 60, 80, 78, 86 ] }
Slice from the Front of the Array
A collection students
contains the following document:
- { "_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.
- db.students.update(
- { _id: 2 },
- {
- $push: {
- scores: {
- $each: [ 100, 20 ],
- $slice: 3
- }
- }
- }
- )
The result of the operation is to slice the elements of the updatedscores
array to the first three elements:
- { "_id" : 2, "scores" : [ 89, 90, 100 ] }
Update Array Using Slice Only
A collection students
contains the following document:
- { "_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:
- db.students.update(
- { _id: 3 },
- {
- $push: {
- scores: {
- $each: [ ],
- $slice: -3
- }
- }
- }
- )
The result of the operation is to slice the elements of the scores
array to the last three elements:
- { "_id" : 3, "scores" : [ 70, 100, 20 ] }
Use $slice with Other $push Modifiers
A collection students
has the following document:
- {
- "_id" : 5,
- "quizzes" : [
- { "wk": 1, "score" : 10 },
- { "wk": 2, "score" : 8 },
- { "wk": 3, "score" : 5 },
- { "wk": 4, "score" : 6 }
- ]
- }
The following $push
operation uses:
- the
$each
modifier to add multiple documents to thequizzes
array, - the
$sort
modifier to sort all the elements of themodifiedquizzes
array by thescore
field in descendingorder, and - the
$slice
modifier to keep only the first threesorted elements of thequizzes
array.
- 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 themodifiers are processed. See Modifiers for details.