-1

I have the following model and I'm trying to update the 'seen' property to true for all of the objects in the notifications array, but I can't figure out a way to do this using one query?

    {
        _id: 'fjkldjasfljsdklfj',
        user_id: 'fjdskjafkldsjadf',
        notifications: [
            {
                seen: false,
                message: 'This is message 1',
                date: '12-03-2021'
            },
            {
                seen: false,
                message: 'This is message 2',
                date: '12-03-2021'
            },
            {
                seen: true,
                message: 'This is message 3',
                date: '12-03-2021'
            },
            {
                seen: false,
                message: 'This is message 4',
                date: '12-03-2021'
            },
        ]
    }
willmahon
  • 319
  • 4
  • 13
  • Does this answer your question? [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – turivishal Jul 19 '21 at 15:14

1 Answers1

2

Use the all positional operator $[]:

db.collection.updateOne(
   { user_id: 'fjdskjafkldsjadf' },
   { $set: { "notifications.$[].seen": true } }
) 
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110