7

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 John (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?

1
  • Just a guess, but it may have been voted down because the documentation states that the value to the update key is a modifier object, which kind of implies the inclusion of the modification instructional key word ($set, $push, etc). I thought your question was a fair once, since I personally find some inconsistencies in the mongodocs use of terminology, particularly with how it refers to various parameter components. Commented May 8, 2012 at 1:35

1 Answer 1

7

Try to use $set like this:

db.messages.findAndModify({query:{'recipients.id':2}, update:{$set:{'recipients.$.isread':true}}})
Sign up to request clarification or add additional context in comments.

2 Comments

Was this helpful? or still an issue?
I haven't tried yet. It looks reasonable, I'll mark it as answer when I try it tomorrow. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.