আপনি এর জন্য $sort() অপারেটরের সাথে aggregate() পদ্ধতি ব্যবহার করতে পারেন। ধারণাটি বুঝতে, আসুন ডকুমেন্টের সাথে একটি সংগ্রহ তৈরি করি। একটি নথির সাথে একটি সংগ্রহ তৈরি করার প্রশ্নটি নিম্নরূপ -
> 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")
} Find() পদ্ধতির সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করুন। প্রশ্নটি নিম্নরূপ -
> 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 }