I know it's possible to update specific array elements, as documented here: https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/
Also, I'm aware it's possible to use a field value to update the value of another field, as explained here: Update MongoDB field using value of another field
What I need is a combination of the two.
Let's say I have this in the DB:
{
a: [
{
aa: 10
},
{
cc: 15
}
]
}
Now, I want to add a field bb
to the array documents with aa
's value, but only if aa
exists. So the output is:
{
a: [
{
aa: 10,
bb: 10
},
{
cc: 15
}
]
}
How can I achieve this?