Why moving limit stage in front of skip stage will be better in performance

why [ {$limit: 10}, {$skip: 15}] performs better than [ {$skip: 15}, {$limit: 10}] in an aggregate pipeline?

Hi @111697,
I am curious, why do you want to do the following?

Can you give me an example use-case?
Because the pipeline mentioned above will always return 0 documents since aggregation pipelines are executed in a step-by-step manner and this pipeline will limit the results to 10 and then it will skip over all those results.

Whereas the later pipeline in your post will return five documents if there are sufficient documents forwarded by the previous stage.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha

You will be certainly interested to read:

And in particular the section:

1 Like