EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MongoDB sort()

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » MongoDB Tutorial » MongoDB sort()

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.

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)4 Online Courses | 2 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,455 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)All in One Data Science Bundle (360+ Courses, 50+ projects)SQL Training Program (7 Courses, 8+ Projects)Oracle DBA Database Management System Training (2 Courses)MS SQL Training (13 Courses, 11+ Projects)

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

MongoDB Training Program (4 Courses, 2 Projects)

4 Online Courses

2 Hands-on Projects

22+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MongoDB Tutorial
  • Commands
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Auto Increment
    • MongoDB Projection
    • Replication in MongoDB
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

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

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

Special Offer - MongoDB Training Program (4 Courses, 2 Projects) Learn More