[英]Pymongo aggregate pipeline
修訂問題。 將很快更新。
演示 - https://mongoplayground.net/p/ksay82IaGHs
按TeacherID
和TeacherID
,得到組合的出現次數,$sort 按occurrence
降序排列。
db.collection.aggregate([
{ $group: { _id: { TeacherID: "$TeacherID", StudentID: "$StudentID" }, occurrence: { $sum: 1 } } },
{ $sort: { "occurrence": -1 } }
]);
[ { "_id": { "StudentID": 2, "TeacherID": 212 }, "occurrence": 3 }, { "_id": { "StudentID": 4, "TeacherID": 223 }, "occurrence": 1 }, ..... ]
如果你想要最高紀錄
演示 - https://mongoplayground.net/p/zBsGdAOdYwy
{ "$limit": 1 }
演示 - https://mongoplayground.net/p/G2KIVcjtYII
如果要檢查特定的 TeacherID,請使用 $match
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.