Perform aggregation sort in MongoDB?


You can use aggregate() method along with $sort() operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.aggregationSortDemo.insertOne({"StudentId":98,"StudentFirstName":"John","StudentLastName":"Smith"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90140c5705caea966c5587")
}
> db.aggregationSortDemo.insertOne({"StudentId":128,"StudentFirstName":"Carol","StudentLastName":"Taylor"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90141b5705caea966c5588")
}
> db.aggregationSortDemo.insertOne({"StudentId":110,"StudentFirstName":"David","StudentLastName":"Miller"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90142f5705caea966c5589")
}
> db.aggregationSortDemo.insertOne({"StudentId":139,"StudentFirstName":"Chris","StudentLastName":"Brown"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90146a5705caea966c558a")
}
> db.aggregationSortDemo.insertOne({"StudentId":125,"StudentFirstName":"Sam","StudentLastName":"Williams"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9015695705caea966c558b")
}
> db.aggregationSortDemo.insertOne({"StudentId":139,"StudentFirstName":"Mike","StudentLastName":"Wilson"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90158e5705caea966c558c")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.aggregationSortDemo.find().pretty();

The following is the output −

{
   "_id" : ObjectId("5c90140c5705caea966c5587"),
   "StudentId" : 98,
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith"
}
{
   "_id" : ObjectId("5c90141b5705caea966c5588"),
   "StudentId" : 128,
   "StudentFirstName" : "Carol",
   "StudentLastName" : "Taylor"
}
{
   "_id" : ObjectId("5c90142f5705caea966c5589"),
   "StudentId" : 110,
   "StudentFirstName" : "David",
   "StudentLastName" : "Miller"
}
{
   "_id" : ObjectId("5c90146a5705caea966c558a"),
   "StudentId" : 139,
   "StudentFirstName" : "Chris",
   "StudentLastName" : "Brown"
}
{
   "_id" : ObjectId("5c9015695705caea966c558b"),
   "StudentId" : 125,
   "StudentFirstName" : "Sam",
   "StudentLastName" : "Williams"
}
{
   "_id" : ObjectId("5c90158e5705caea966c558c"),
   "StudentId" : 139,
   "StudentFirstName" : "Mike",
   "StudentLastName" : "Wilson"
}

Here is the query for MongoDB aggregation sort.

Case 1 − Whenever you want the result in descending order. The query is as follows −

> db.aggregationSortDemo.aggregate( {$group: {_id: '$StudentId',"TotalOccurrences": {$sum: 1}}}, {$sort: {_id: -1}} ).pretty();

The following is the output:

{ "_id" : 139, "TotalOccurrences" : 2 }
{ "_id" : 128, "TotalOccurrences" : 1 }
{ "_id" : 125, "TotalOccurrences" : 1 }
{ "_id" : 110, "TotalOccurrences" : 1 }
{ "_id" : 98, "TotalOccurrences" : 1 }

Case 2 − Whenever you want the result in ascending order. The query is as follows −

> db.aggregationSortDemo.aggregate( {$group: {_id: '$StudentId', "TotalOccurrences": {$sum: 1}}}, {$sort: {_id: 1}} ).pretty();

The following is the output −

{ "_id" : 98, "TotalOccurrences" : 1 }
{ "_id" : 110, "TotalOccurrences" : 1 }
{ "_id" : 125, "TotalOccurrences" : 1 }
{ "_id" : 128, "TotalOccurrences" : 1 }
{ "_id" : 139, "TotalOccurrences" : 2 }

Updated on: 30-Jul-2019

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements