【问题标题】:Update object into array MongoDB将对象更新到数组 MongoDB
【发布时间】:2016-04-08 22:20:39
【问题描述】:

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

{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}
  },{}]
}

【问题讨论】:

    标签: arrays mongodb mongodb-query mongo-shell


    【解决方案1】:

    试试这个:

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

    或者

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

    这里 foo.0 是索引。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多