Looking to update the object values of an array in a document, but first needing to find its ID. Here is the structure of the document:
{
_id: ObjectId('12312')
notifications: [
{
_id: ObjectId('1')
status: 'unread'
},
{
_id: ObjectId('2')
status: 'unread'
},
{
_id: ObjectId('3')
status: 'unread'
}
]
}
The goal is to update the status of notifications under that user where the object ID matches those in the result array.
result =
[
new ObjectId("1"),
new ObjectId("2"),
]
This should produce the following result after the update:
{
_id: ObjectId('12312')
notifications: [
{
_id: ObjectId('1')
status: 'read'
},
{
_id: ObjectId('2')
status: 'read'
},
{
_id: ObjectId('3')
status: 'unread'
}
]
}
Here is the current progress made, but the desired outcome is not achieved yet:
const updateUser = await User.updateMany({_id:idofuser, "notifications._id": { $in : result }},{
$set: {
"notifications.$.status": "read",
}
})