Before sorting your data, ensure that you're using the correct database. You can switch to your database with the following command:
use myDatabase
Let’s assume you have a students
collection containing the following documents:
db.students.find().pretty()
Sample Data:
[
{ "_id": 1, "name": "Alice", "age": 22, "score": 85 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 },
{ "_id": 3, "name": "Charlie", "age": 23, "score": 88 },
{ "_id": 4, "name": "David", "age": 24, "score": 92 }
]
MongoDB sorts data in ascending order (default) when you pass the value 1
. You can sort by any field, such as age
or score
.
age
in ascending order:db.students.find().sort({ age: 1 })
✅ Result: This will return the documents ordered by the age
field in ascending order (youngest first).
Output:
[
{ "_id": 1, "name": "Alice", "age": 22, "score": 85 },
{ "_id": 3, "name": "Charlie", "age": 23, "score": 88 },
{ "_id": 4, "name": "David", "age": 24, "score": 92 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 }
]
To sort in descending order, pass the value -1
in the sort()
method.
score
in descending order:db.students.find().sort({ score: -1 })
✅ Result: This will return the documents ordered by the score
field in descending order (highest score first).
Output:
[
{ "_id": 4, "name": "David", "age": 24, "score": 92 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 },
{ "_id": 3, "name": "Charlie", "age": 23, "score": 88 },
{ "_id": 1, "name": "Alice", "age": 22, "score": 85 }
]
You can sort by multiple fields. When sorting by multiple fields, the first field will have the highest priority, and the second field will be used for tie-breaking.
age
in ascending order, then by score
in descending order:db.students.find().sort({ age: 1, score: -1 })
✅ Result: This will return documents ordered by age
in ascending order, and if two or more documents have the same age, they will be sorted by score
in descending order.
Output:
[
{ "_id": 1, "name": "Alice", "age": 22, "score": 85 },
{ "_id": 3, "name": "Charlie", "age": 23, "score": 88 },
{ "_id": 4, "name": "David", "age": 24, "score": 92 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 }
]
You can combine sorting with queries to filter and sort the data at the same time.
age
greater than 22 and sort them by score
in descending order:db.students.find({ age: { $gt: 22 } }).sort({ score: -1 })
✅ Result: This will find students older than 22 and sort them by score
in descending order.
Output:
[
{ "_id": 4, "name": "David", "age": 24, "score": 92 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 },
{ "_id": 3, "name": "Charlie", "age": 23, "score": 88 }
]
You can limit the number of results returned by combining the limit()
method with sort()
.
score
in descending order:db.students.find().sort({ score: -1 }).limit(2)
✅ Result: This will return the top 2 students with the highest scores.
Output:
[
{ "_id": 4, "name": "David", "age": 24, "score": 92 },
{ "_id": 2, "name": "Bob", "age": 25, "score": 90 }
]
After executing the queries, always verify the results using
db.students.find().pretty()
Sorting results in MongoDB is a simple but powerful feature that allows you to organize your data in a meaningful way.
✅ Single-field Sorting: Use 1
for ascending and -1
for descending.
✅ Multiple-field Sorting: Combine multiple fields for more complex ordering.
✅ Query and Sort Together: Filter and sort data in a single query.
✅ Limit Results: Use limit()
to restrict the number of results returned.
By mastering sorting in MongoDB, you can enhance your queries and efficiently retrieve organized data! 🌟