【问题标题】:Add Object to array of multiple documents将对象添加到多个文档的数组
【发布时间】:2015-09-08 23:36:30
【问题描述】:

我在向 Mongo 集合中添加对象时遇到了一些问题。这是我的更新功能:

'readMessages': function (chatId) {

    Messages.update({
        "chat_id": chatId,
        "read.user_id": {$nin: [this.userId]}
    }, {$addToSet: {'read.$.user_id': this.userId}});


}

我想查找用户尚未阅读的所有聊天消息。当我调用该函数时,他应该更新所有消息并将 user_id 插入“读取”数组。

如果我使用我的值执行 Messages.find(),他会找到 150 条消息 - 这是正确的。但是当我尝试使用下面的命令更新所有消息对象时,什么都没有发生 - 我的“读取”数组仍然是空的。

Mongo 将新对象推送到所有匹配文档的正确语法/方式是什么?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    认为我自己找到了解决方案:

       'readMessages': function (chatId) {
    
        Messages.update({
            "chat_id": chatId,
            "read.user_id": {$nin: [this.userId]}
        }, {$addToSet: {'read': {user_id: this.userId}}}, {validate: false, multi: true});
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多