复杂的Mongo更新

时间:2013-10-24 09:48:52

标签: mongodb

该集合如下: { 'id' : 'xxx', 'items' : [ { 'index' : 1, 'on' : '' }, { 'index' : 2, 'on' : '' } ] }

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

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

有没有办法在一次通话中更新所有内容?

1 个答案:

答案 0 :(得分:0)

db.collection.update({id:'xxx', items:{$elemMatch:{index:2}}}, {$set:{'items.$.on':true}})
相关问题