Updated May 8, 2023
Introduction to order by in MongoDB
By operator sorts the result of the query in an ascending and descending order, ascending and descending order is based on the value we mentioned in the query that is 1 is for ascending order and -1 is for descending order. Order by the command is deprecated in MongoDB shell. MongoDB shell provides cursor.sort() method to query the database.
Syntax:
db.collection.find().sort({field:value})
The “db” stands for database, and the collection stands for collection name we have created under database, find is to find out value from the collection and sort is to be sorting the field, the value from the collection. Sort function requires to complete sorting within 32 megabytes.
How order by Works in MongoDB?
Order by return value from the collection in ascending or descending order. When we mention field: value pair, it will return value from a given field with chosen descending or ascending order. But we use sort () method, to display value by order wise. We will see a sorting example in the below context.
Examples to Implement order by in MongoDB
Below are the examples of implementing order by in MongoDB:
Example #1
We have created the company database.
Code:
use company
Example #2
We have created a collection under a database company.
Code:
collection_name.insert({field;value})
Example #3
We have inserted a document into a collection.
Code:
db.employee.insert({emp_name:'radhika',age:24,dept:'prod',salary:5000})
Output:
nInserted: 1 – record inserted successfully.
Example #4
We will sort the record by salary-wise in ascending order.
Code:
db.employee.find().sort({salary:1})
Salary is a field, and 1 is valued as we want to display data in ascending order.
Explanation to the above output: We can observe in the above image salary is ordered in ascending order as 4000,5000,6000,7000 and 9000.
Example #5
We will sort the salary in descending order with value -1
Code:
employee.find().sort({salary:-1})
Salary is a field, and -1 is valued as we want to display data in descending order.
Explanation to the above output: We can observe in the above image salary is ordered in descending order as 9000,7000,6000,5000 and 4000.
Example #6
We can apply to sort on string field with the same syntax, and we need to use the same command.
Code:
db.employee.find().sort({emp_name:1})
We can observe in the below image, emp_name is displayed in ascending alphabetic order.
Example #7
We can limit the document while sorting it with LIMIT(). We can mention limit value or can keep it empty.
Code:
db.employee.find().sort({field:value}).limit()
Output:
Example #8
If we don’t mention the value in the limit, all data will be fetched.
Code:
db.employee.find({},{_id:0}).sort({emp_name:1}).limit()
Output:
Example #9
While sorting if we don’t mention field: value pair, data will be fetched in ascending order because ascending order is the default order.
We will sort the data by AGE because we have to provide value in find().
Code:
db.employee.find({},{_id:0,age:1}).sort({})
Output:
Example #10
If we don’t provide any value in find(), default data will be fetched.
Code:
db.employee.find().sort({})
We can observe in the below image, and data is fetched order by with _id.
Output:
Example #11
If we mentioned -1 value in find(), and 1 value in sort(), data will be fetched as per sorting value given.
Code:
db.employee.find({},{dept:1,_id:0}).sort({dept:-1})
Dept is sorted in ascending order.
Output:
Conclusion
We have studied or learned what is the order by in mongo shell and due to deprecation in the mongo shell cursor.sort() method, we are using to sort the collection. This will help users to get the data as per their choice of ascending or descending order. We can limit the collection using the limit() method.
Recommended Articles
This is a guide to order by MongoDB. Here we discuss Syntax how order by in MongoDB works? And different examples with proper codes and outputs. You can also go through our other related articles to learn more –