user :["id": 1, userObj:[{"bookid": 1, "library":"Oxford", "taken":true}, {"bookid": 2, "library":"Cambridge", "taken":true}]]
I would like to update the value taken and set it to false for the bookid.
tmp = await User.findOneAndUpdate({id: req.body.id}, { $elemMatch: {'userObj.bookid': req.body.bookid }, { $set: { 'userObj.taken': false }, }, { new: true } )
CodePudding user response:
You need to use positional operators
db.collection.update({
id: 1,
"userObj.bookid": 1
},
{
$set: {
"userObj.$.taken": false //$ is a positional operator which updates the matching array element
},
})