User:
{
_id: "userID",
array_1:[],
array_2:[]
}
I need to do two updates to this document:
array_1contains objects with uniqueidproperty. I need to find one whereid=targetIDand replace it with thenewObject, like this:
User.updateOne(
{
_id: userID,
"array_1.id": targetID
},
{
"$set":{"array_1.$":newObject}
}
)
array_2contains objects, where multiple objects can have sameidvalues. I need to update 2 properties of all objects in thearray_2whereid=targetID, like this:
User.updateOne(
{
_id: userID,
"array_2.id": targetID
},
{
"$set": {
"array_2.$[elem].property_1": new_property_1,
"array_2.$[elem].property_2": new_property_2
}
},
{
"arrayFilters": [{ "elem.id": targetID }],
"multi": true
}
)
These two work fine if I run it separately, but how do I combine both in one function?