I know a document exists for an update operation I want to perform. Also I know one of the array elements exists. I want to update that known array element and an element in another array that potentially might require different match criteria. Both arrays contain objects.
Can I use a single update operation to update both arrays when the match criteria for the 2 arrays might be different?
Does “arrayFilters” allow you to get an array index for 2 different arrays?
Sample documents BEFORE update
When array1 does not have match criteria (no prizeId = prize1) matching element for array2
For both of the sample documents above, I would like to update the element in array2, identified by “prizeId = prize1”, as well as make sure an element matching “prizeId = prize1” in array1 is updated, or array1 is appended if array1 does not contain a matching element.
Nice to e-meet you and thank you for trying to help!
Unfortunately I don’t think you understood the challenge. I want to update a single document. I provided 2 examples of the state of that single document. BOTH examples should have the SAME RESULT after a single update operation. The “first document remained unchanged” is NOT desired.
Also please note I made an edit to array1 in example 1. array1 is not empty, but lacks the same criteria as array2.
@steevej Your answer helped me arrive at a final solution. I use your 2-filter technique to get an index to each array for $set, and one of the filters uses the $or operator to handle the 2 scenarios array1 can be in.