Push multiple objects into MongoDB array document | Append Multiple Objects to an Array in MongoDB

Normally we use $push to push or append the element or object into an array in mongoDB. But sometime we may need to push the multiple objects into mongoDB array document. Here below I'll try to explain how we can do that.

Let say I have a collection of favorite products and the collection name is favorite_products.

Now in the above document, I wanted to push/append the multiple user objects into the users array whose product id is 124.



An array of the object which I wanted to push is given below:

I'll use $each with $push  to push/append the multiple object into mongoDB array and the query is given below:

 

Share This:

2 thoughts on “Push multiple objects into MongoDB array document | Append Multiple Objects to an Array in MongoDB

    • Admin

      use $addToSet. The only difference between $push and $addToSet is that $addToSet won't update the document if it already exist while $push will simply push and update the document.

Leave a Reply

Your email address will not be published. Required fields are marked *