Sort() Method in MongoDB

Learn via video courses
Topics Covered

Overview

The sort() method in MongoDB is used to provide the ordering for the documents returned by the query as a result. This method can be applied before fetching the documents from the collection. Sort in mongoDB takes the document as a parameter which has the field: value, here field specifies the name of the field based on which ordering of documents is performed and the value specifies the sorting order. Value can take 1 or -1 value, 1 means ascending and -1 means descending order.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction to $sort in MongoDB

All the input documents are sorted by the $sort in MongoDB, it takes the document as an argument for specifying the sorting field and order of sorting. Sorting field and order is provided in the form of field : order and we can specify 1 for ascending order and -1 for descending order.

Behavior

Limits: Sorting can be performed on 32 maximum keys.

Sort Consistency: MongoDB does not maintain any particular order for storing documents in a collection. When the sorting is performed in documents having duplicate values, then those documents having duplicate values can be returned in any order. If you want consistent sorting then for sorting at least one field having all unique values must be provided. And we can easily achieve this by using the _id field for the document sorting.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Syntax

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Parameters

<field>: <sort order> pair in the form of a document is passed as a parameter where the field specifies the name of the field and we can pass 1 or -1 as a sort order, and the sort order specifies the order of sorting. 1 means ascending order for sorting and 0 means descending sorting order.

Return values

Queried documents are returned by it in sorted order.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Here we are taken an example of the Student collection, so we create a collection and insert some documents into it.

student collection database

  • Now fetching the student in ascending order of the age.

Query:

Output:

output of student database

  • Fetching the student in descending order of the age.

Query:

Output:

output of student database sort

  • Sorting the student in ascending order of age and sort in ascending order of name if two have the same age.

Query:

Output:

sort on age and name

  • Fetching the student in ascending order of the name.

Query:

Output:

sort on name

  • Fetching the student in descending order of the name.

Query:

Output:

student datebase sort on name

FAQs

Q: Does the original stored order of the documents modified by the sort in MongoDB?

A No, the order of original documents of the collection is not modified by the sort(). It only returns the sorted order of the documents as the result of the query.

Q: What is the sort in MongoDB?

A Sort in MongoDB is used for sorting the documents of the collection based on specified fields and sorting order. It returns the query result in the sorted form of documents.

Q: How MongoDB sort() method can be used?

A: For using the MongoDB sort() method, we need to pass the fields to be used for sorting and sorting order in the form of field: sorting_value within the document.

Syntax: db.collectionName.sort({<field1>: <sort order>, <field2>: <sort order> ...})

Conclusion

  • The sort() method in MongoDB is used to provide the ordering for the documents returned by the query as a result.
  • Sort in MongoDB takes the document as an argument for specifying the sorting column and order of sorting.
  • Sorted documents are returned by sort in MongoDB.
  • Order of original documents of the collection is not modified by the sort().