我在mongodb中有以下数据:
{
"articleId": 1,
"QuliatyScore": 80,
"RelvanceToTech": 50,
"FeedbackAuthor":"Jack"
},
{
"articleId": 1,
"QualityScore": 80,
"RelvanceToTech": 50,
"FeedbackAuthor":"Tom"
},
{
"articleId": 2,
"QualityScore": 60,
"RelvanceToTech": 30,
"FeedbackAuthor":"Jack"
},
{
"articleId": 2,
"QualityScore": 70,
"RelvanceToTech": 40,
"FeedbackAuthor":"Tom"
}
...
我想获得前3名的平均分(从所有作者中选择前3名Avg(QualityScore + FeedbackAuthor)文章
答案 0 :(得分:0)
这是聚合查询(在MongoDB 3.0上测试):
[
{
$project:{
articleId:"$articleId",
score:{
$add:[
"$QualityScore",
"$RelevanceToTech"
]
}
}
},
{
$group:{
_id:"$articleId",
avgScore:{
$avg:"$score"
}
}
},
{
$sort:{
avgScore:-1
}
},
{
$limit:3
}
]
说明: