我在这里有一点问题,我无法弄明白。
我试图根据3个最佳分数获得玩家的平均分数。
这就是我现在正在做的事情:
var divideBy = 3;
var sortedBoard = Spark.runtimeCollection('matchResults').aggregate(
{$match: {"leadId": boardId}},
{$group: {
_id: "$playerId",
played: { $sum: 1 },
points: { $sum: { $add : [ '$data.leadTotals.stfBonus', '$data.leadTotals.placePoints', '$data.leadTotals.startPoints' ] }},
}
},
{$project:{
type: "$SortBoard",
avgPoints: { $divide: [ "$points", divideBy ]},
sumPoints: "$points",
played : "$played"
}},
{$sort:{"avgPoints":-1}},
{$limit : divideBy}
);
我的文档看起来像这样(这只来自一个玩家,但我的收藏中有多个玩家):
{
"_id": {
"$oid": "5b203c8f031f5bc44d520bd3"
},
"playerId": "5b1eac79031f5bc44d51b4c1",
"data": {
"leadTotals": {
"stfBonus": 2,
"placePoints": 3,
"startPoints": 3
}
},
{
"_id": {
"$oid": "5b203c8f031f5wert8909wetr"
},
"playerId": "5b1eac79031f5bc44d51b4c1",
"data": {
"leadTotals": {
"stfBonus": 4,
"placePoints": 8,
"startPoints": 3
}
},
{
"_id": {
"$oid": "5b203c8f0ælkj56j6lkj756"
},
"playerId": "5b1eac79031f5bc44d51b4c1",
"data": {
"leadTotals": {
"stfBonus": 1,
"placePoints": 1,
"startPoints": 3
}
},
{
"_id": {
"$oid": "5b203c8f031sdgfjklkj3457"
},
"playerId": "5b1eac79031f5bc44d51b4c1",
"data": {
"leadTotals": {
"stfBonus": 7,
"placePoints": 2,
"startPoints": 3
}
}
现在,“avgPoints”应该是:11,6666667(doc 1,2,4);
但我现在使用的代码,$ limit设置错误的地方我想。它现在的作用是,如果我的收藏中有超过3个用户,它只会显示3个最佳用户而不是其他用户。它还将用户的所有分数之和除以最佳分数。
我可以将此更改为仅获得每位用户的3个最佳分数吗?
希望得到帮助并提前致谢:-)