Sort() Method in MongoDB
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
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Syntax
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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
Examples
Here we are taken an example of the Student collection, so we create a collection and insert some documents into it.

- Now fetching the student in ascending order of the age.
Query:
Output:

- Fetching the student in descending order of the age.
Query:
Output:

- Sorting the student in ascending order of age and sort in ascending order of name if two have the same age.
Query:
Output:

- Fetching the student in ascending order of the name.
Query:
Output:

- Fetching the student in descending order of the name.
Query:
Output:

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




