My question is, will this work, given that tests.grade is a field inside of an array of embedded documents? Would it know to use the embedded doc that was matched in the elemMatch? What if there are multiple matches? Would this be optimized as in ESR rule?
The $sort aggregation stage should work as expected, even if tests.grade is a field inside an array of embedded documents. When using $elemMatch inside $match, it returns the first element that matches the specified condition in the array, and only that element is used in the next pipeline stages.
Regarding multiple matches, $sort should work as expected, sorting all the matched embedded documents The order of the documents after the sort operation will depend on the sort order specified in the $sort stage. To confirm this, I tried to make a sample collection from the sample document you provided. This is what the documents looked like:
Regarding the optimization of the query, if you have an index on “tests.grade”, then the query should perform well. MongoDB’s query optimizer should use the index to speed up the sort operation, which will improve the query’s performance. However, the specific optimization strategy may depend on the size of the collection, the number of matches, and the sort order. You can use explain output to check this all. The explain output for the above aggregation query without any index looked like this:
As we can see, the inputStage has no COLLSCAN this time and there is an IXSCAN happening. Of course, there may be some changes to your explain output based on the full structure of your documents and the exact aggregation query that you’re using. I would suggest using explain output to understand which indexes your query is using. Also, using compass for writing aggregation queries can help a lot too since one can easily see the output after each stage and analyze accordingly.