$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.

MongoDB $push Operator

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

Similar Reads

$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....

Syntax

The syntax for using the $push operator is:...

MongoDB $push Operator

In the following examples, we are working with...

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