【问题标题】:How to update each document with one query in mongodb [duplicate]如何在mongodb中使用一个查询更新每个文档[重复]
【发布时间】:2019-12-18 23:58:02
【问题描述】:

我有如下数据。

// first document
  {
    "id": 1,
    "exist": true
  }
// second document
  {
    "id": 2,
    "exist": false
  }
// third document
  {
    "id": 3,
    "exist": false
  }

当我 findOneAndUpdate({_id:2}),{exist:true}) 时,我希望 'id:1' 的存在在一个使用聚合等的查询中自动更改为 false。
你能推荐一些想法吗?非常感谢您阅读我的问题。

【问题讨论】:

标签: mongodb mongoose


【解决方案1】:

我真的不明白你想在你的收藏中更新什么,但要更新许多文档使用updateMany

db.collection.updateMany(
      { violations: { $gt: 1 } }, // the filter to select the wanted document
      { $set: { "exist" : true } } // the change to apply to the field
   );

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2020-04-10
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2020-07-15
    • 2020-07-27
    相关资源
    最近更新 更多