更新数组中的嵌套对象值,数组中有多个对象。
{
"_id" : ObjectId("5a7e395e20a31e44e0e7e284"),
"name" : "a",
"address" : [
{
"street" : "123",
"town" : "bar"
},
{
"street" : "Lower Street",
"town" : "bar"
},
{
"street" : "123",
"town" : "foo"
}
]
}
如果街道为 123,则需要更新城镇为 HNO。
使用 位置过滤运算符
$[<identifier]
更新数组中的匹配元素。
db.collection.updateMany({
"address.street": "123"
},
{
$set: {
"address.$[a].town": "HNO"
}
},
{
arrayFilters: [
{
"a.street": "123"
}
]
})