【问题标题】:Updating embedded mongo array values更新嵌入的 mongo 数组值
【发布时间】:2014-09-24 22:27:02
【问题描述】:

我目前有一个像这样设置的 mongo 对象。

{
        "_id" : "FtFKS23swBSEtEJAK",
        "active_users" : [
                {
                        "user" : {
                                "profile" : {
                                        ...
                                }
                        },
                        "maxMessages" : 25
                },
                {
                        "user" : {
                                "profile" : {
                                        ...
                                }
                        }
                        "maxMessages" : 25
                }
        ],
        "display_name" : "Testing"
}

现在,我正在尝试运行一个 mongo 查询,该查询将通过嵌入式活动用户并将 maxMessages 增加给定值。

但我似乎无法做到这一点。

这是我失败的尝试

activeUsersLength = Rooms.findOne({}).active_users.length

for i in [0..activeUsersLength-1]
    Rooms.update({_id: room}, {$inc: {'active_users.' + i + '.maxMessages': 1}})

【问题讨论】:

标签: mongodb coffeescript


【解决方案1】:
Rooms.update({_id:room, "active_users.user": { $exists : true } },{$inc:{"active_users.$.maxMessages":1}  })

我没有测试过

看看 http://docs.mongodb.org/manual/reference/operator/projection/positional/#proj.S

感谢@dgiugg 的评论,您无法更新数组https://jira.mongodb.org/browse/SERVER-1243 中的更多元素

【讨论】:

  • 我之前试过这个。抛出的错误是Exception while invoking method 'insertMsg' MongoError: Cannot apply the positional operator without a corresponding query field containing an array.
  • @SeanCallahan 我已经更新了,你可以试试吗?当我可以在我的 cpu 上测试它时
  • 它不起作用:stackoverflow.com/questions/4669178/…,另见jira.mongodb.org/browse/SERVER-1243。它只会更新数组的第一个元素。
猜你喜欢
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 2021-07-20
  • 2017-01-05
  • 2022-01-24
相关资源
最近更新 更多