इसके लिए आप $sort() ऑपरेटर के साथ एग्रीगेट () मेथड का इस्तेमाल कर सकते हैं। अवधारणा को समझने के लिए, आइए हम दस्तावेज़ के साथ एक संग्रह बनाते हैं। दस्तावेज़ के साथ संग्रह बनाने की क्वेरी इस प्रकार है -
> 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")
} संग्रह से सभी दस्तावेज़ों को ढूँढें () विधि की सहायता से प्रदर्शित करें। क्वेरी इस प्रकार है -
> db.aggregationSortDemo.find().pretty();
निम्न आउटपुट है -
{
"_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"
} यहाँ MongoDB एग्रीगेशन सॉर्ट के लिए क्वेरी है।
केस 1 - जब भी आप परिणाम को अवरोही क्रम में चाहते हैं। क्वेरी इस प्रकार है -
> db.aggregationSortDemo.aggregate( {$group: {_id: '$StudentId',"TotalOccurrences": {$sum: 1}}}, {$sort: {_id: -1}} ).pretty(); निम्न आउटपुट है:
{ "_id" : 139, "TotalOccurrences" : 2 }
{ "_id" : 128, "TotalOccurrences" : 1 }
{ "_id" : 125, "TotalOccurrences" : 1 }
{ "_id" : 110, "TotalOccurrences" : 1 }
{ "_id" : 98, "TotalOccurrences" : 1 } केस 2 - जब भी आप परिणाम को आरोही क्रम में चाहते हैं। क्वेरी इस प्रकार है -
> db.aggregationSortDemo.aggregate( {$group: {_id: '$StudentId', "TotalOccurrences": {$sum: 1}}}, {$sort: {_id: 1}} ).pretty(); निम्न आउटपुट है -
{ "_id" : 98, "TotalOccurrences" : 1 }
{ "_id" : 110, "TotalOccurrences" : 1 }
{ "_id" : 125, "TotalOccurrences" : 1 }
{ "_id" : 128, "TotalOccurrences" : 1 }
{ "_id" : 139, "TotalOccurrences" : 2 }