EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB sort()
 

MongoDB sort()

Priya Pedamkar
Article byPriya Pedamkar

Updated March 3, 2023

MongoDB sort()

 

 

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.

Watch our Demo Courses and Videos

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

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:

MongoDB sort() output 1

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 ()

MongoDB sort() output 2

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:

MongoDB sort() output 3

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:

output 4

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:

output 5

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

output 6

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

output 7

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 –

  1. MongoDB Collection
  2. order by in MongoDB
  3. What is MongoDB?
  4. Is MongoDB Open Source
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
EDUCBA

*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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW