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.
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:
We have created the company database.
We have created a collection under a database company.
We have inserted a document into a collection.
nInserted: 1 – record inserted successfully.
We will sort the record by salary-wise in ascending order.
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.
We will sort the salary in descending order with value -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.
We can apply to sort on string field with the same syntax, and we need to use the same command.
We can observe in the below image, emp_name is displayed in ascending alphabetic order.
We can limit the document while sorting it with LIMIT(). We can mention limit value or can keep it empty.
If we don’t mention the value in the limit, all data will be fetched.
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().
If we don’t provide any value in find(), default data will be fetched.
We can observe in the below image, and data is fetched order by with _id.
If we mentioned -1 value in find(), and 1 value in sort(), data will be fetched as per sorting value given.
Dept is sorted in ascending order.
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.
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 –