MongoDB – $push Operator

MongoDB provides different types of array update operators to update the values of the array fields in the documents and $push operator is one of them. This operator is used to append a specified value to an array.

Syntax:

{ $push: { <field1>: <value1>, ... } }

Here, <field> can specify with dot notation in embedded/nested documents or an array.

  • If the specified field in the $push operator is not present in the document, then this operator will add the array field with the value as its items.
  • The $push operator insert items at the end of the array.
  • If the specified field in the $push operator is not an array, then this operation will fails.
  • If the value of the $push operator is an array, then this operator will append the whole array as a single element. And if you want to add each item of the value separately, then you can use $each modifier with $push operator.
  • You can use this operator with methods like update(), findAndModify(), etc., according to your requirement.

We can also use the following modifiers with the $push operator :
Syntax:

{ $push: { <field1>: { <modifier1>: <value1>, ... }, ... } }

The processing of the push operation with modifiers works in the following order:



  • First update the array to add items in the correct position.
  • Second, apply sort if specified.
  • Third slice the array if specified.
  • Fourth store the array.

Note: Here the order in which the modifiers appear in the $push operator does not matter.

Modifier Description
$each It is used to append multiple values to the array field.
$slice It is used to limit the number of array items and require the use of the $each modifier.
$sort It is used to order items of the array and require the use of the $each modifier.
$position It is used to specify the location in the array at which to insert the new items and require the use of the $each modifier. If the $push operator does not use $position modifier, then this operator will append the items to the end of the array.

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.

Appending a single value to an array:

In this example, we are appending a single value, i.e., “C++” to an array field, i.e., language field in the document that satisfy the condition(name: “Rohit”).

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Rohit"}, {$push: {language: "C++"}})

chevron_right


Appending multiple values to an array:

In this example, we are appending multiple values, i.e., [“C”, “Ruby”, “Go”] to an array field, i.e., language field in the document that satisfy the condition(name: “Sumit”).

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Sumit"}, {$push: {language: {$each: ["C", "Ruby", "Go"]}}})

chevron_right


Appending multiple values to an array in the nested/embedded document:

In this example, we are appending multiple values, i.e., [89, 76.4] to an array field, i.e., personal.semesterMarks field of a nested/embedded document.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Sumit"}, 
                      {$push: {"personal.semesterMarks": {$each: [89, 76.4]}}})

chevron_right


Use of modifiers with $push operator:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Rohit"},
                      {$push: { language: { $each: ["C", "Go"],
                                $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.