Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Perform aggregation sort in MongoDB?
To perform aggregation sort in MongoDB, use the aggregate() method with the $sort stage. The $sort stage orders documents by specified fields in ascending (1) or descending (-1) order.
Syntax
db.collection.aggregate([
{ $group: { ... } },
{ $sort: { field: 1 } } // 1 = ascending, -1 = descending
]);
Sample Data
db.aggregationSortDemo.insertMany([
{"StudentId": 98, "StudentFirstName": "John", "StudentLastName": "Smith"},
{"StudentId": 128, "StudentFirstName": "Carol", "StudentLastName": "Taylor"},
{"StudentId": 110, "StudentFirstName": "David", "StudentLastName": "Miller"},
{"StudentId": 139, "StudentFirstName": "Chris", "StudentLastName": "Brown"},
{"StudentId": 125, "StudentFirstName": "Sam", "StudentLastName": "Williams"},
{"StudentId": 139, "StudentFirstName": "Mike", "StudentLastName": "Wilson"}
]);
{
"acknowledged": true,
"insertedIds": [...]
}
Display all documents to verify the data ?
db.aggregationSortDemo.find();
{
"_id": ObjectId("..."),
"StudentId": 98,
"StudentFirstName": "John",
"StudentLastName": "Smith"
}
{
"_id": ObjectId("..."),
"StudentId": 128,
"StudentFirstName": "Carol",
"StudentLastName": "Taylor"
}
{
"_id": ObjectId("..."),
"StudentId": 110,
"StudentFirstName": "David",
"StudentLastName": "Miller"
}
{
"_id": ObjectId("..."),
"StudentId": 139,
"StudentFirstName": "Chris",
"StudentLastName": "Brown"
}
{
"_id": ObjectId("..."),
"StudentId": 125,
"StudentFirstName": "Sam",
"StudentLastName": "Williams"
}
{
"_id": ObjectId("..."),
"StudentId": 139,
"StudentFirstName": "Mike",
"StudentLastName": "Wilson"
}
Case 1: Descending Order Sort
Group by StudentId and sort results in descending order ?
db.aggregationSortDemo.aggregate([
{ $group: { _id: "$StudentId", "TotalOccurrences": { $sum: 1 } } },
{ $sort: { _id: -1 } }
]);
{ "_id": 139, "TotalOccurrences": 2 }
{ "_id": 128, "TotalOccurrences": 1 }
{ "_id": 125, "TotalOccurrences": 1 }
{ "_id": 110, "TotalOccurrences": 1 }
{ "_id": 98, "TotalOccurrences": 1 }
Case 2: Ascending Order Sort
Group by StudentId and sort results in ascending order ?
db.aggregationSortDemo.aggregate([
{ $group: { _id: "$StudentId", "TotalOccurrences": { $sum: 1 } } },
{ $sort: { _id: 1 } }
]);
{ "_id": 98, "TotalOccurrences": 1 }
{ "_id": 110, "TotalOccurrences": 1 }
{ "_id": 125, "TotalOccurrences": 1 }
{ "_id": 128, "TotalOccurrences": 1 }
{ "_id": 139, "TotalOccurrences": 2 }
Key Points
- Use
1for ascending order and-1for descending order. - The
$sortstage typically comes after$groupto sort the aggregated results. - Multiple fields can be sorted by adding more field-value pairs in the
$sortobject.
Conclusion
The $sort stage in MongoDB aggregation pipelines orders grouped results by specified fields. Use 1 for ascending and -1 for descending order to control the output sequence.
Advertisements
