I want to update a value of a sub-document where a message has a specific id and an user id is in a recipient array. I want to update a value of the matched object with the specified user id.
When I run the following query on the MongoDB CLI, everything works and the value is updated:
db.getCollection('messages').update({
_id : ObjectId("57d7edb8c497a75a6a7fde60"),
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set : {"recipients.$.read": true}
});
But when I run the following query via JS in my FeathersJS application:
messageService.update({
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set: {"recipients.$.read": true}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
I get the error:
GeneralError: The positional operator did not find the match needed from the query. Unexpanded update: recipients.$.read
What am I doing wrong?
And is there a better way of updating many messages at once?
Thanks!