Computer >> Máy Tính >  >> Lập trình >> MongoDB

Thực hiện sắp xếp tổng hợp trong MongoDB?

Bạn có thể sử dụng phương thức tổng hợp () cùng với toán tử $ sort () cho việc này. Để hiểu khái niệm, chúng ta hãy tạo một bộ sưu tập với tài liệu. Truy vấn để tạo một bộ sưu tập với một tài liệu như sau -

> 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")
}

Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find (). Truy vấn như sau -

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

Sau đây là kết quả -

{
   "_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"
}

Đây là truy vấn cho loại tổng hợp MongoDB.

Trường hợp 1 - Bất cứ khi nào bạn muốn kết quả theo thứ tự giảm dần. Truy vấn như sau -

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

Sau đây là kết quả:

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

Trường hợp 2 - Bất cứ khi nào bạn muốn kết quả theo thứ tự tăng dần. Truy vấn như sau -

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

Sau đây là kết quả -

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