How can I use aggregate to update a field if a field with value exists in an array?
orders: [
{ id: 100, status: "shipped", options: [{ returned: true }] },
{ id: 101, status: "packed", options: [{ quick: true }] },
{ id: 102, status: "ordered" }
]
desired result: [
{ id: 100, status: "returned", options: [{ returned: true }] }, // <- updated status
{ id: 101, status: "packed", options: [{ quick: true }] },
{ id: 102, status: "ordered" }
]
I can set an additional field, but I have not managed a conditional update, tried with $set
and $cond
but it would not work together.
$set {
status: { options: {$elemMatch: {returned: true}}}
}
Thanks,
bluepuma