I have a document with the following structure:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-3ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: 1
622ew3e041bac3272qwe4441: 1
I want to update this structure to this:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-43ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: {a:1, b:false}
622ew3e040bac3272qwe4441: {a:1, b:false}
I am having trouble with iterating the notification
objects fields as its is random hex string.
I have tried using following solutions but not able to get the expected result
MongoDB Aggregation pipeline: use of For loop
CodePudding user response:
db.collection.update({},
[
{
$set: {
notifications: {
$arrayToObject: {
$map: {
input: { $objectToArray: "$notifications" },
as: "n",
in: {
k: "$$n.k",
v: { a: "$$n.v", b: false }
}
}
}
}
}
}
],
{
multi: true
})