Adding new values in an array using updateMany()

I am trying to add new values to an array on all documents in a collection. I have tried this:

db.accounts_test.updateMany(
  {
    $push: {
      friends:{
        $each:[
          'Fred', 
          'Shaggy'
         ]
       }
     } 
  }, 
  {}
)

I am getting this error: Update document requires atomic operators

According to this page, $push is a valid update operator.

How should this query be constructed?

The query part, { } must be the first parameter. The update part, { $push : … } ,must be the second parameter. You got them in the wrong order.

2 Likes

Silly me, I don’t know how I missed that. Thank you.

You would not believe how may times I said that myself.

You welcome. Please make sure you mark my post as the solution. So that the thread can eventually by closed. This gives other readers confidence that the recommendations are working.