anda dapat menggunakan $[]
positional-all
untuk memperbarui elemen array bersarang, mongo doc untuk $[]
permintaan
db.t14.update(
{},
{ $set: { "calc.$[].datasets.$[elem].name": "updated" } },
{ arrayFilters: [ { "elem": "x1" } ], multi: true}
)
koleksi sampel dengan dokumen
> db.t14.findOne()
{
"_id" : 1,
"calc" : [
{
"preset" : "abc",
"datasets" : [
{
"_id" : "x1",
"name" : "n1"
},
{
"_id" : "x2",
"name" : "n2"
}
]
}
]
}
perbarui
> db.t14.update({},{$set: { "calc.$[].datasets.$[elem].name": "newname" } },{ arrayFilters: [{ "elem.name": "n1" }], multi: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
posting pembaruan
> db.t14.findOne()
{
"_id" : 1,
"calc" : [
{
"preset" : "abc",
"datasets" : [
{
"_id" : "x1",
"name" : "newname"
},
{
"_id" : "x2",
"name" : "n2"
}
]
}
]
}
>