MongoDB Sort() & Limit() Query with Order By Examples
โก Smart Summary
MongoDB Sort() and Limit() query modifiers control how many documents a query returns and in what order. The limit() method caps the result count, while sort() arranges documents in ascending or descending order using values of 1 or -1.

What is Query Modifications?
Mongo DB provides query modifiers such as the ‘limit’ and ‘Orders’ clause to provide more flexibility when executing queries. We will take a look at the following query modifiers
MongoDB Limit Query Results
This modifier is used to limit the number of documents which are returned in the result set for a query. The following example shows how this can be done.
db.Employee.find().limit(2).forEach(printjson);
Code Explanation
- The above code takes the find function which returns all of the documents in the collection but then uses the limit clause to limit the number of documents being returned to just 2.
Output
If the command is executed successfully, the following Output will be shown
The output clearly shows that since there is a limit modifier, so at most just 2 records are returned as part of the result set based on the ObjectId in ascending order.
MongoDB Sort by Descending Order
One can specify the order of documents to be returned based on ascending or descending order of any key in the collection. The following example shows how this can be done.
db.Employee.find().sort({Employeeid:-1}).forEach(printjson)
Code Explanation
- The above code takes the sort function which returns all of the documents in the collection but then uses the modifier to change the order in which the records are returned. Here the -1 indicates that we want to return the documents based on the descending order of Employee id.
If the command is executed successfully, the following Output will be shown
Output
The output clearly shows the documents being returned in descending order of the Employeeid.
Ascending order is defined by value 1.
MongoDB Sort by Ascending Order
Just as sort() can return documents in descending order, it can also return them in ascending order. Ascending order is defined by the value 1 in the sort object. This arranges documents from the smallest value to the largest, which is useful for listing records from oldest to newest or from A to Z.
The following example sorts every employee document by Employeeid in ascending order:
db.Employee.find().sort({Employeeid:1}).forEach(printjson)
Here is how the query works:
- find() retrieves all documents in the Employee collection.
- sort({Employeeid:1}) orders the results by the Employeeid field, from the lowest id to the highest.
- forEach(printjson) prints each document in readable JSON.
When you run this command, the documents appear in increasing order of Employeeid. Ascending is also the default direction, so leaving the value as 1 gives a low-to-high order on the chosen field. To reverse the result, simply change the value from 1 to -1, and you can also chain limit() to return only the first few sorted records.
MongoDB Skip() for Pagination
The skip() method tells MongoDB to ignore a given number of documents before returning the rest. On its own, skip() is rarely useful, but combined with limit() it creates pagination, letting you display results one page at a time.
The following example skips the first two employees and returns the next two:
db.Employee.find().skip(2).limit(2).forEach(printjson)
To build page-by-page navigation, follow this pattern:
- Decide the page size, for example 2 documents per page.
- Multiply the page size by the page number minus one to get the skip value.
- Pass that value to skip() and the page size to limit().
For instance, page 1 uses skip(0).limit(2), page 2 uses skip(2).limit(2), and page 3 uses skip(4).limit(2). Always add a sort() as well, so that the order of documents stays stable across pages. Note that very large skip values can be slow on big collections, because MongoDB still scans and discards the skipped documents; range-based pagination is faster at scale.
Sort by Multiple Fields in MongoDB
MongoDB can sort by more than one field at a time. You list the fields in the sort object in priority order, and MongoDB sorts by the first field, then uses the next field to break ties, and so on.
The following example sorts employees by name in ascending order, and for employees with the same name, by Employeeid in descending order:
db.Employee.find().sort({EmployeeName:1, Employeeid:-1}).forEach(printjson)
The order of keys in the sort object matters: EmployeeName is applied first because it appears first. This multi-key sorting is useful for reports where you group by one field and then rank within each group. For the best performance on large collections, create a compound index that matches the fields and directions used in the sort.


