Initial Document Structure: Suppose a document has two array fields, array1 and array2.
First $unwind: After unwinding array1, if array1 has n elements, you get n documents.
Second $unwind: If you then unwind array2, and if array2 has m elements, each of the n documents will be multiplied by m, resulting in n * m documents.
When a $group stage is then applied to sum a field, this multiplication of documents can lead to overcounting, as each original document is now represented multiple times.
I would like to kindly ask for a generally accepted practice/ pattern to avoid this. Perhaps the choice of using 2 unwinds is the issue? Or can the $group stage be modified to tackle this?
I am not really sure but I would guess that it is when whatever you want to do cannot be done with $map, $filter or $reduce you use $unwind. It is a good choice when you do not have any other choice.