Let say we have a document in mongodb
{
id : 101,
addresses : [
{
id: 1,
suite: "flat 201",
street: "north street"
city: "london",
country: "uk"
}
]
}
i want to update multiple properties of the object at once without replacing whole object. For example :
const newAddress = {
id:1,
suite: "flat 301",
street: "south street"
}
userCollection.updateOne(
{id : 101},
{ $set : {"addresses.$[address]" : newAddress } },
{arrayFilters: [{address.id : newAddress.id}]}
)
This operation sets the object with new fields instead of updating only the given fields.
"addresses.$[address].suite" : newAddress.suite, "addresses.$[address].street" : newAddress.street.