Question:
I have a mongodb document in this format. There are multiple collections of arrays as given here. How do I get all elements where SID = 100.Answer:
Option 1:- Unwind all arrays and nested arrays.
- Match only the subobjects with SID:100
- Project only the necessary object content
Playground1
Option 2: ( Slightly faster)
- Unwind the first 3x arrays
- Match only documents having at least 1x SID:100 in 4th array
- Filter only those from 4th array having SID:100
- Unwind the 4th array
- Project only the final object we need
Playground2
If you have better answer, please add a comment about this, thank you!