You can use aggregation array operator like $filter or $reduce to compare array elements and return the result. These array operators work like iterators (like iterating in a for-loop on a JavaScript or Java array field). See the examples, for the two operators in the MongoDB Manual.
The $elemMatch cannot be used within the $expr, as $expr takes only the aggregate operators as arguments.
@Prasad_Saya thank you for the feedback. It is really helpful! now I have gone to the documentation and chose the $filter operator and wrote this query:
However, my question is how do I compare the score for the two types of entries (ie type: “exam” and type: “quiz”) so that I filter only “exam” scores which are greater than “quiz” scores for the same student. Help and feedback is much appreciated
Verify the output, and the $filter returns an array. And you can get the first element of the array using the $arrayElemAt operator, and use the value in the next stage.
In the next step, get the exam scores greater than the quiz score:
@Prasad_Saya , I appreciate the help. After pondering on the idea of using some arrays operators I have finally found the solution in the $slice operator. The query below gave me the results I expected! It is amazing!! Thank you