簡體   English   中英

Pymongo 聚合管道

[英]Pymongo aggregate pipeline

修訂問題。 將很快更新。

演示 - https://mongoplayground.net/p/ksay82IaGHs

TeacherIDTeacherID ,得到組合的出現次數,$sort 按occurrence降序排列。

db.collection.aggregate([
  { $group: { _id: { TeacherID: "$TeacherID", StudentID: "$StudentID" }, occurrence: { $sum: 1 } } },
  { $sort: { "occurrence": -1 } }
]);

Output
 [ { "_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM