我有一個具有以下結構的檔案:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-3ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: 1
622ew3e041bac3272qwe4441: 1
我想將此結構更新為:
id: 6229d3fd40bac327243e4ww2
userID: "594e710c-c632-43ae-929a-f13e294ee6ef"
appID: "622994e2b98225609b89affd"
notifications:Object
6229d3e040bac3272qwe4441: {a:1, b:false}
622ew3e040bac3272qwe4441: {a:1, b:false}
我在迭代notification物件欄位時遇到問題,因為它是隨機的十六進制字串。我嘗試使用以下解決方案,但無法獲得預期的結果
MongoDB 聚合管道:使用 For 回圈
uj5u.com熱心網友回復:
db.collection.update({},
[
{
$set: {
notifications: {
$arrayToObject: {
$map: {
input: { $objectToArray: "$notifications" },
as: "n",
in: {
k: "$$n.k",
v: { a: "$$n.v", b: false }
}
}
}
}
}
}
],
{
multi: true
})
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440581.html
