1

I have almost no experience in SQL or noSQL.

I need to update every document so that my fields "Log*" are under the new field "Log"

I found some help from this StackOverflow, but I am still wondering how to move the data.

Thank you very much

Original document

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

Final Document

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

1 Answers1

0

This requires MongoDB 4.2 or higher:

db.<collection>.updateMany({}, [
  {$set: {"Log.LogPath": "$LogPath", "Log.LogTypeList": "$LogTypeList"}}, 
  {$unset: ["LogPath", "LogTypeList"]}
])
maf-soft
  • 2,335
  • 3
  • 26
  • 49