Doubt about order or SORT, SKIP, LIMIT when using aggregation framework!

Hi everyone,

I have some doubt about the order of $sort $skip and $limit in the aggregation framework:

In the course “MongoDB for nodejs developers” when talking about the Sort, Skip, Limit using the NodeJS API it says that it does not matter the order we apply to our cursors in our code, because MongoDB will always 1st SORT, 2nd SKIP and 3rd LIMIT independently of which order we specify.

I know this behavior DON’T apply when using the aggregation framework, where you are given an ordered list of stages, and therefore the order in which we specify $skip and $limit matters. But what I can’t understand is how it is possible to do a $limit before a $sort (the video in this course states in that case the server is able to do a “ top-k sort ” algorithm)?

In a video of a different course of MongoDB which talked also about aggregation framework (maybe it is outdated) it stated “ it doesn’t make any sense to skip and limit, unless you first sort ”. Here I let you the link to that video:

That seems to contradict what it is said about the order for a “top-k sort”, can someone shed some light into this?


These links may be helpful:

José Carlos