Does the order of FindOptions matter?

Hi all,

Will I get different results based on the order of FindOptions chained to my find() query?
I.e. will db.alphabet.find().limit(2).order({letter: 1}) return something else than db.alphabet.find().order({letter: 1}) .limit(2) (assuming I created documents in a random, unordered fashion)?
I’m currently watching a tutorial, and it looked like it might matter. Unfortunately, the docs I linked above don’t say (or I missed it)…

Hi Christian,

The order in which limit and sort are set on the query cursor doesn’t affect how the query is executed. MongoDB will always run sort first and then limit.

Considering the situation where the limit is executed first, it’s not very useful because it’s impossible to know what you are limiting.

1 Like

the options are set on client once, and then sent to servers for processing in one go. So the ordering in your code never matters.

1 Like

Do you know where in the docs that’s explained, by any chance? :innocent:

I took a look at cursor’s documentation for sort and limit and this information is not there. I have this knowledge from internal training.

Take a look at

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.