MongoDB – $slice Modifier

MongoDB provides different types of array update operators to update the values of the array fields in the documents and $slice modifier is one of them. This modifier is used to limit the number of array items during a $push operation.

Syntax:

{
  $push: {
     <field>: {
       $each: [ <value1>, <value2>, ... ],
       $slice: <number>
     }
  }
}
  • If the value of number is zero, then this modifier will update the array field to an empty array.
  • If the value of number is negative, then this modifier will update the array field to contain only the last number items.
  • If the value of number is positive, then this modifier will update the array field to contain only the first number items.
  • $slice modifier must appear with $each modifier in the $push operator. You are allowed to pass an empty array in the $each modifier which help $slice modifier to show its effect. If you use the $slice modifier without $each modifier, then you will get an error.
  • The order in which the modifiers appear in $push operation is immaterial.

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: two documents that contain the details of the contributor in the form of field-value pairs.



Slicing the array from the end:

In this example, we are adding new items in the language field and then use $slice modifier to trim the array to the last four items.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Suman"},
                      {$push: {language: { $each: ["SQL", "JS++"], 
                       $slice: -4}}})

chevron_right


Slicing the array from the front:

In this example, we are adding new items in the language field and then use $slice modifier to trim the array to the first five items.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Rohit"}, 
                      {$push: {language: { $each: ["R Language", "JS++"],
                       $slice: 5}}})

chevron_right


Updating an array using $slice modifier:

In this example, we are updating the array of the language field by triming the array to the last three items.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Rohit"},
                      {$push: {language: { $each: [], 
                        $slice: -3}}})

chevron_right


Using $slice modifier with other modifiers with $push operator:

In this example, we are using the $slice modifier with other modifiers like $each and $sort with $push operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Rohit"},
                      {$push: { language: { $each: ["C++", "Kotlin"], 
                       $sort: 1, $slice: 4}}})

chevron_right


Here,

  • The $each modifier is used to add multiple documents to the language array.
  • The $sort modifier is used to sort all the items of the modified language array in ascending.
  • The $slice modifier is used to keep only the first four sorted items of the language array.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.