Chapter 5 Quiz 1 sort() and limit'()

Hello,
I don’t understand how this query:

db.companies.find({ "founded_year": { "$ne": null }},
              { "name": 1, "founded_year": 1 }
             ).limit(5).sort({ "founded_year": 1 })

can be a valid answer as we limit before sorting. How can we be sure to have the oldest companies first?

Hey @Cedric_MONCADE,

The order in which you chain the limit() and the sort() methods are not significant. It will always consider sort() first and then limit() in the case of MQL. Also explained in our M001 lecture here. Both statements return the first five oldest documents, as determined by the sort order on ‘founded_year’.

You can read more here: Combine Cursor Methods

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

Thanks,
Kushagra Kesav,
Curriculum Services Engineer

2 Likes

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