【问题标题】:Add field to every document with existing data (move fields data to new field)使用现有数据向每个文档添加字段(将字段数据移动到新字段)
【发布时间】:2019-05-02 23:07:28
【问题描述】:

我几乎没有 SQL 或 noSQL 方面的经验。

我需要更新每个文档,以便我的字段“Log*”位于新字段“Log”下

我从this StackOverflow 那里找到了一些帮助,但我仍然想知道如何移动数据。

非常感谢

原始文件

// collection: Services { "_id" : ObjectId("5ccb4f99f4953d4894acbe79"), "Name" : "WebAPI", "LogPath" : "Product\\APIService\\", "LogTypeList" : [ { "Name" : "ApiComCounter", "FileName" : "ApiComCounter.log" }, { "Name" : "ApiService", "FileName" : "ApiService.log" } ] }

最终文件

// collection: Services { "_id" : ObjectId("5ccb6fa2ae8f8a5d7037a5dd"), "Name" : "InvoicingService", "Log" : { "LogPath" : "Product\\APIService\\", "LogTypeList" : [ { "Name" : "ApiComCounter", "FileName" : "ApiComCounter.log" }, { "Name" : "ApiService", "FileName" : "ApiService.log" } ] } }

【问题讨论】:

    标签: mongodb nosql field


    【解决方案1】:

    这需要 MongoDB 4.2 或更高版本:

    db.<collection>.updateMany({}, [
      {$set: {"Log.LogPath": "$LogPath", "Log.LogTypeList": "$LogTypeList"}}, 
      {$unset: ["LogPath", "LogTypeList"]}
    ])
    

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2020-07-04
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2017-02-14
      • 2019-11-12
      • 2011-07-16
      相关资源
      最近更新 更多