Odd unit test: sorting comments by date during lookup

Just going through the Get Comments ticket and this particular unit test was giving me a massive headache.
In the end I just tried running mflix and found out that it’s not required in order to validate that ticket, but it has got me thinking: How would you ensure that the comment field was sorted during the join?

Also, I’m a bit curious as to why the localField/foreignField syntax wasn’t mentioned; it’s so much simpler!.

Edit: Figured it out now, the pipeline lookup seems to be a lot more flexible. Just needed to sort within that after the inner $match.


Hi There,

I am still not able to figure out where to add the $sort code.
‘$sort’: {
‘comments.date’: -1

I tried after the $lookup, also after inner $match still no luck. ANy help is greatly appreciated. thanks

After the inner $match is correct. I think the issue you are having is that the lookup is already looking at comments, so you need to sort on 'date' rather than 'comments.date'.

1 Like

thank you that works.