Database IntegrationLesson 65Free preview
MongoDB Sort
This lesson is part of Node.js · All levels
Summary
This lesson explains the sequencing of MongoDB query results from Node.js using the sort() method. You can sort it based on one or more columns with a value of 1 for ascending and -1 for descending. Sorting is often combined with limiting to get, for example, the most recent or expensive data.
Key points
- The order of the ascending, the descending.
- It can sort multiple columns at once; prioritize the key sequence of the object.
- Sorting works on the cursor before the result is drawn, and is most efficient when there is an index.
- Combine with limit() for cases like '10 largest'.
- Fields that are not present in some documents are considered to be null valuable when sorting.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.