EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial order by in MongoDB
 

order by in MongoDB

Priya Pedamkar
Article byPriya Pedamkar

Updated May 8, 2023

order by in MongoDB

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

order by in MongoDB - 1

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.

order by in MongoDB - 2

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.

order by in MongoDB - 3

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.

order by in MongoDB - 4

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:

LIMIT()

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:

fetched

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:

data by age

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:

order by in MongoDB -8

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:

value in find()

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 –

  1. Mongo DB Create Database
  2. How To Install MongoDB
  3. MongoDB Alternatives
  4. Is MongoDB Open Source
  5. PostgreSQL ORDER BY | How to Work?
  6. Guide to MongoDB Limit()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW