将具有现有数据的字段添加到每个文档(将字段数据移动到新字段)

问题描述 投票:1回答:1

我几乎没有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个回答
0
投票

这需要MongoDB 4.2或更高版本:

db.<collection>.updateMany({}, [
  {$set: {"Log.LogPath": "$LogPath", "Log.LogTypeList": "$LogTypeList"}}, 
  {$unset: ["LogPath", "LogTypeList"]}
])
© www.soinside.com 2019 - 2024. All rights reserved.