Updated March 3, 2023
Introduction to MongoDB sort()
MongoDB sort () method is used to sort the data or collection result in ascending or descending order at the same time. The sort() method acknowledges a particular filed of documents holding the collection of the database includes sorting order. If we have not set any value or giving priority to the particular value then by default the sorting method would automatically sort the data or collection result in ascending order.
Syntax
Below is the syntax of the MongoDB sort method.
collection_name.find ().sort ({Field_key: 1})
collection_name.find ().sort ({Field_key:-1})
collection_name.find ().sort ({})
Below is the parameter description of the above syntax.
- Collection name – Collection name is known as the name given to the uniquely specified mongodb documents.
- Find – Find method is used to retrieve the result from the collection as well as used to retrieve the result from the table.
- Sort – Sort method in MongoDB is used to sort the data in ascending or descending order.
- Field key = 1 – If we set field key is 1 then sort result data is in ascending order.
- Field key = -1 – If we set field key is -1 then sort result data is in descending order.
How does sort() method work in MongoDB?
Below is the working of the sort method in MongoDB.
In the below example we have not used or specified any sorting order to sort the emp_sort collection data. But it will automatically sort in ascending order using sort method.
Code:
db.emp_sort.find ({}, {"emp_id": 1}).sort ({})
Output:
Examples of MongoDB sort()
Below are the examples of mongoDB sort():
Example of data description of emp_sort table using the sort method in MongoDB.
db.emp_sort.find ()
We can sort the data by using -1 and 1. Where Minus 1(-1) is used to sort data in descending order and 1 is used to sort data in ascending order.
Let’s see the below examples for better understanding.
Example #1 – Sort data in ascending order
In the below example we have to sort collection data in ascending order using order value as 1.
Code:
db.emp_sort.find ().sort ({emp_id:1})
Output:
Example #2 – Sort data in descending order
In the below example we have to sort collection data in ascending order using order value as -1.
Code:
db.emp_sort.find ().sort ({emp_id:-1})
Output:
Example #3 – Sort data in ascending order without using order
In the below example we have to sort collection data in ascending order. But, here We have not used any order value to sort data.
Code:
db.emp_sort.find ().sort ({})
Output:
Sorting using LIMIT() Method in MongoDB
The Limit() method is used to limit the data or collection result in mongodb.
This uses only one parameter or a number to determine how many documents we need to retrieve as a result of the collection.
Often we have large quantities of data or collection in the mongodb database but we need to retrieve a few units of data in such instances limit method is being used.
Let’s see some examples to get a better understanding of the limit() method.
Example #1
In the below example we have to sort the data in ascending order by using the limit method.
Code:
db.emp_sort.find().sort({emp_id:1}).limit (5)
Output
Example #2
In the below example we have to sort the data in descending order by using the limit method.
Code:
db.emp_sort.find ().sort ({emp_id:-1}).limit (5)
Output
Conclusion
In the above article, we have learned about sort() method in mongodb along with the working and examples. The sorting method is the document that is used to specify the collection result in the mongodb database. We may also use field keys to more precisely conduct sorting. It includes a limit() method to limit the collection result. So, the sort() method is very relevant in mongodb, if we have not determined sort() method then the mongodb does not ensure the collection result order.
Recommended Articles
This is a guide to MongoDB sort(). Here we discuss how does sort() method works in MongoDB with programming examples for understanding better. You may also have a look at the following articles to learn more –