How to update the speicific field in array of objects in mongoose

Viewed 37

Hi there i wanted to update status field in messages. I'm trying to use

db.messagev2.updateMany(
  {
    user1: 8, user2:150,
   "messages.receiver":150,
   "messages.status" : "sent",
    },
{'$set': {'messages.$.status': "read"}},{multi:true}) 

but only the first status in message is updating .. Need help enter image description here

1 Answers

You need to replace:

  'messages.$.status' 

with:

  'messages.$[elem].status'

And add $arrayFilters :

    { arrayFilters: [ { "elem.receiver": 150 ,"elem.status": "sent"} ] }
Related