w3resource

MongoDB Array Update Operator - $push

Description

In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.

At the time of updating if the value itself is an array, the $push operator appends the whole array as a single element.

If you want to add each element of the value separately, the $push operator can be used with the $each modifier.

Syntax:

db.collection.update( <query>,{ $push: { <field>: <value> } })

Parameters:

Name Description
field name of the column or field to the document..
value... These are the values to be specified for the fields or columns.
query The query may be an expression or condition or criteria.

Sample collection "student"

{
        "_id" : 1,
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "maths",
                "gkn",
                "stat",
                "astro"
        ],
        "achieve" : [
                70,
                87,
                90,
                90,
                65,
                81
        ]
}

Example of MongoDB $push operator

If we want to append 95 to the array field achieve against the condition subjects is "gkn", the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" },{ $push: { "achieve": 95 } });

Here in the above example the value 95 will be append into the array achieve, because the condition mentioned is matching for this operation.

To see the newly updated document -

> db.student.find().pretty();

Output of the command:

{
        "_id" : 1,
        "achieve" : [
                70,
                87,
                90,
                90,
                65,
                81,
                95
        ],
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "maths ",
                "gkn",
                "stat",
                "astro"
        ]
}

Example of MongoDB $push operator when field is not an arrays

If we want to append 2 in sem field which is not an array type field, the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" },{ $push: { "sem": 2 } });

Here in the above example the sem field is not an array type field, so the operation will not execute and give the following output

Output

{Cannot apply $push/$pushAll modifier to non-array

Example of MongoDB $push operator using $each modifiers

If we want to append multiple elements or more than one elements (77,49,83 ) to the array achieve for the document student for the condition subjects is "gkn", the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" },{ $push: { "achieve": {$each : [77,49,83 ]} } });

Here in the above example the $each modifier have used to append multiple elements 77,49,83 to the array achieve which matches the condition subjects equals "gkn".

To see the newly updated document -

> db.student.find().pretty();

Output of the command:

{
        "_id" : 1,
        "achieve" : [
                70,
                87,
                90,
                90,
                65,
                81,
                77,
                49,
                83
        ],
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "maths ",
                "gkn",
                "stat",
                "astro"
        ]
}

Previous: $pullAll
Next: Aggregation Pipeline Operators



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/mongodb-array-update-operator-$push.php