MongoDB $push Operator

MongoDB $push operator appends a value to the already existing array within a document.

$push Operator in MongoDB

The $push operator in MongoDB is used to update the array in MongoDB. It inserts a new value into the array in the specific index.

If the mentioned field is absent in the document to update, the $push operator adds it as a new field and includes the mentioned value as its element.

If the value is an array, the $push operator appends the whole array as a single element. To add each element of the value separately, the $push operator can be used with the $each modifier.

Syntax

The syntax for using the $push operator is:

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

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

We can also use the 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.

List of $push operator modifiers

Below are the modifiers in MongoDB that can be used with $push operator.

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.

MongoDB $push Operator

In the following examples, we are working with

Database: w3wiki
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 example using $push operator

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”).

Query:

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

Output:

Appending multiple values to an array using $push operator

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”).

Query:

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

Output:

Appending multiple values to an array in the nested/embedded document using $push operator:

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.

Query:

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

Output:

Using modifiers with $push operator example

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

Query:

db.contributor.update({name: &quot;Rohit&quot;},
{$push: { language: { $each: [&quot;C&quot;, &quot;Go&quot;],
$sort: 1, $slice: 4}}})

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.

Output:

KeyTakeAways About MongoDB $push Operator

  • The $push operator in MongoDB is used append a new value into the array in the specific index.
  • 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.
  • You can use this operator with methods like update(), findAndModify(), etc., according to your requirements.
  • The $push operator inserts items at the end of the array.
  • If the specified field in the $push operator is not an array, then this operation will fail.
  • If the value of the $push the 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 the operator.

Contact Us