I have a collection with documents of this schema:
{
_id: something,
recipients: [{id:1, name:"Andrey", isread:false}, {id:2, name:"John", isread:false}]
}
Now, I want to update "isread" for 开发者_StackOverflowJohn (id = 2) using findAndModify()
, because I also need to get the original document.
I'm trying this command:
db.messages.findAndModify({query:{'recipients.id':2}, update:{'recipients.$.isread':true}})
but what it does, it just replaces the whole "recipients" field with 'recipients.$.isread', so the document now looks like:
{
_id: someid,
'recipients.$.isread':true
}
What am I doing wrong?
Try to use $set like this:
db.messages.findAndModify({query:{'recipients.id':2}, update:{$set:{'recipients.$.isread':true}}})
精彩评论