Minimongo 嵌套查询嵌入文档

Minimongo nested query embed documents

我只想在 mongodb 和 return 中搜索我要查找的嵌入文档。 这是文档:

"_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"
    }
]

我想要的只是 "messages.isSeen" 等于 false 并且接收者不是 Meteor.user().username.

最后是如何更新该字段使其变为真。

希望有人能帮忙!提前致谢 !

你需要这样的东西:

Chat.update({
  'messages.isSeen': false,          // isSeen is false
  'messages.receiver': {             // Receiver is
     $ne: Meteor.user().username     // not equal to Meteor.user().username
  }
}, {
  'messages.$isSeen': true           // Set isSeen to true
});

您需要在查询中包含 _id 并在更新中包含 $set

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 }});