You can first group by questionId
, then sort the result and select the top 5 records.
db.collection.aggregate([{
"$group": {
_id: "$questionId",
userId: {
$first: "$userId"
},
commentId: {
$first: "$commentId"
},
questionId: {
$first: "$questionId"
},
count: {
$sum: 1
}
},
},
{ $sort: { "count": -1 } },
{ $limit: 5 },
])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…