0

集合是这样的: { 'id' : 'xxx', 'items' : [ { 'index' : 1, 'on' : '' }, { 'index' : 2, 'on' : '' } ] }

我可以像这样进行更新: collection.update( { 'id' : 'xxxx', 'items.index' : '2' }, { '$set' : { 'on' : 'true' } )

但是,问题是,如果我想在一个文档中更新多个项目(每个项目可能具有不同的键“on”值)。然后我必须做一个循环。

有没有办法在一个电话中更新所有内容?

4

1 回答 1

0
db.collection.update({id:'xxx', items:{$elemMatch:{index:2}}}, {$set:{'items.$.on':true}})
于 2013-10-24T10:37:35.997 回答