i want to search into an embed document in mongodb and return only what i'm looking for. Here's the document:
"_id" : "yH8HmCPz6H6E8Hinq",
"between" : [
"4bgdLrztpqgwAkZP4",
"9jZhXHjAkoY7mmX7B"
],
"messages" : [
{
"content" : "fdsqf",
"user" : "4bgdLrztpqgwAkZP4",
"created_at" : ISODate("2016-11-17T23:13:59.659Z"),
"isSeen" : false,
"sender" : "John doe",
"receiver" : "Elen doe"
},
{
"content" : "test",
"user" : "9jZhXHjAkoY7mmX7B",
"created_at" : ISODate("2016-11-20T11:42:42.893Z"),
"isSeen" : false,
"sender" : "Elen doe",
"receiver" : "John doe"
}
]
All what i want to have is "messages.isSeen" equals to false and receiver isn't Meteor.user().username.
And finally how to update that field to become true.
Hope someone can help ! Thanks in advance !
You need to include the _id
in the query and $set
in the update.
let id = "yH8HmCPz6H6E8Hinq";
let username = Meteor.user().username;
let query = { _id: id, messages: { $elemMatch: { isSeen: false, receiver: { $ne: username }}}};
Chat.update(query,{ $set: { "messages.$.isSeen": true }});