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 1 for ascending order and -1 for descending order.
  • The $sort stage typically comes after $group to sort the aggregated results.
  • Multiple fields can be sorted by adding more field-value pairs in the $sort object.

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.

Updated on: 2026-03-15T00:16:58+05:30

314 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements