I want to update the array of objects "profileSpheres". If "profileSpheres.idq" is equal to "Q0800" and "profileSpheres.ida" is equal to "A0810_001", then "profileSpheres.ida" must be "A0800_001"
This is the link to the playground
Example of document:
{
"_id": 0,
"idu": "AA0092",
"profileSpheres": [
{
"idq": "Q0780",
"ida": "A0780_002",
"value": null,
"explain": null,
"date": "2023-12-26T04:03:57.196Z"
},
{
"idq": "Q0800",
"ida": "A0810_001",
"value": null,
"explain": "encorvar la espalda mucho rato o usar mochilas pesadas ",
"date": "2023-12-26T04:04:37.679Z"
},
{
"idq": "Q0810",
"ida": "A0810_002",
"value": null,
"explain": null,
"date": "2023-12-26T04:04:37.755Z"
},
{
"idq": "Q0820",
"ida": "A0820_002",
"value": null,
"explain": null,
"date": "2023-12-26T04:04:40.411Z"
}
]
}
And this is my code:
db.collection.updateMany({
"idu": "AA0092"
},
{
"$set": {
"profileSpheres.$[elem].ida": "A0800_001"
},
"arrayFilters": [
{
"elem.idq": "Q0800"
},
{
"elem.ida": "A0810_001"
}
]
})
The arrayFilters
should be outside of the update {}
. Try this:
db.collection.update(
{"idu": "AA0092"},
{"$set": {"profileSpheres.$[elem].ida": "A0800_001"}},
{"arrayFilters": [
{
"elem.idq": "Q0800",
"elem.ida": "A0810_001"
}
]}
)
See How it works on the mongoDB playground