将对象更新为数组MongoDB

时间:2016-04-08 22:20:39

标签: arrays mongodb mongodb-query mongo-shell

假设您的收藏中有以下文件:

{foo:[{
    bar1:{_id:1, active: true},
    bar2:{_id:2, active: true}
  },{}]
}

查询:

db.test.update({$or: [
    {'foo.bar1._id': 1)},
    {'foo.bar2._id': 1}
  ]
},
{$set: {'foo.?????.active': false}})

结果:

{foo:[{
    bar1:{_id:1, active: false},
    bar2:{_id:2, active: true}
  },{}]
}

1 个答案:

答案 0 :(得分:0)

试试这个:

HashWithIndifferentAccess

或者

db.test.update({"foo.0.bar1._id" : 1}, {$set : {"foo.0.bar1.active" : "false"}})

这里foo.0是索引。

感谢。

相关问题