【问题标题】:Update a Collection to add a new Field with the value of another Field of same Collection in bulk mood更新一个集合以批量添加一个具有相同集合的另一个字段的值的新字段
【发布时间】:2020-12-09 09:26:20
【问题描述】:

我对 mongoDB 有点陌生。假设我有一个结构如下的集合:

mycoll
{ 
    _id, 
    col1 -> NumberInt
}

集合中已经有数千份文档。现在,我想更新集合的每个文档以添加 NumberInt 类型的新字段(即 col2),其值将与字段 col1 的值相同。有没有什么办法可以批量处理,而不是为每个文档运行更新命令?我目前使用的是 4.4.1 版本的 mongoDB

【问题讨论】:

    标签: mongodb bulkupdate


    【解决方案1】:

    您可以使用 Mongo updateMany() 功能。

    db.collection.updateMany(
       <filter>,
       <update>,
       {
         upsert: <boolean>,
         writeConcern: <document>,
         collation: <document>,
         arrayFilters: [ <filterdocument1>, ... ],
         hint:  <document|string>        // Available starting in MongoDB 4.2.1
       }
    )
    

    查看此链接了解更多详情, Update Many documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2023-01-11
      • 2022-12-20
      • 1970-01-01
      相关资源
      最近更新 更多