【发布时间】:2012-06-14 20:02:42
【问题描述】:
我正在关注the example in the docs:
> t.find()
{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }
> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
> t.find()
{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
这显示了如何搜索 Joe 的投票计数并增加它。我想要完成的是在查询 Joe 时增加 Jane 的投票计数。在我的例子中,comments 是一个长度始终为 2 的数组。也许最好用我当前的(非工作)语法来展示它:
t.update( {'comments.by':'joe'}, {$inc:{'comments[1-$].votes':1}}, false, true )
我认为$ 在这里应该是0,因为 Joe 在数组中排在第一位。为了获得另一个数组索引,我假设 1-$ 可以完成这项工作,但事实并非如此。
如何匹配另一个数组元素而不是查询的元素?
【问题讨论】:
标签: arrays mongodb matching positional-operator