Thank you so much, these are some very complex aggregations and I appreciate the time taken to come up with them.
However, for the first aggregation, it doesn’t return the count at the specific index of array2 but rather returns an array of count, as such (in this example, my document had 3 elements in array2, with a count of 1,2,3 respectively):
{
countValue: [1,2,3]
}
But this is always the same regardless of the index specified in $arrayElemAt under the array2_member var. I tried index 0,1,2 but the output is always the same, where I would be expecting countValue: 1, countValue: 2, countValue: 3 respectively as you described.
The second pipeline works well!
However - what if I only know the index of the element in array2 that I want to update? This pipeline requires the _id of the element in array2.