I would like to mark all messages as read by 'jim'. Here is the structure of a thread:
db.threads.save({
messages: [
{
read_by: ['bob', 'jim']
},
{
read_by: ['bob']
},
{
read_by: ['bob']
}
]
})
As you can see, one message has already been read by 'jim', the rest only by 'bob'. I'd like to find and modify any embedded documents so that 'jim' is appended to the read_by
array.
Here is where I got:
db.threads.findAndModify({
query: {
'messages.read_by': {
$ne: 'jim'
}
},
update: {
$push: {
'messages.$.read_by': 'jim'
}
}
})
I get this error:
uncaught exception: findAndModifyFailed failed: "can't append to array using string field name [$]"
The query works with a db.threads.find()
so I guess the problem is with the update part of the findAndModify()
call.