【问题标题】:Update specific field in all documents in MongoDB [duplicate]更新 MongoDB 中所有文档中的特定字段 [重复]
【发布时间】:2017-09-13 05:54:30
【问题描述】:

我在 MongoDB 中有一个包含对象列表的集合。每个对象都有一个“更新器”数组,它们是具有我要更新的字段的对象。该数组中的一个更新程序需要更新描述。我怎样才能只更新更新程序对象的描述,我知道“updaterId”。

更新:

在我看来,设置标志 multi: true 将在 Mongo 2.2+ 中发挥作用。

db.configurations.update(
  {"updaters.updaterId": "90391154-67BB-452E-A1A7-A07A98B94F86"},
  {$set: {"updaters.$.description": "This tool will prevent users from Unloading Linked Revit files for \"all users\" which causes such Linked File to be unloaded by default when opening project."}},
  {multi: true}
)

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以像这样更新描述 updaterId:

    db.collection.update( {"updaters.updaterId" : updaterId } ,
                   {$set : {"updaters.$.description" : "My description"}} ,
                   false ,
                   true);
    

    【讨论】:

    • 这不是只更新一个条目吗?我有多个具有相同 updaterId 的对象,我试图一次更改它们。这可能吗?
    • 暂时没有。使用 $in 也只会更新数组中的第一个 id。
    • 所以我建议你使用循环。如果您的数据集很大,您可以使用 mongo 批量更新。
    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2019-02-27
    • 2016-02-15
    • 2014-06-01
    • 2022-01-20
    • 2020-10-02
    • 2021-07-24
    相关资源
    最近更新 更多