On this page本页内容
{
$top:
{
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
sortBy | $sort.$sort。
| |
output |
Null和缺少值Consider the following aggregation that returns the top document from a group of scores:考虑以下从一组分数中返回顶级文档的聚合:
$top$topnull。db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: 2 },
{ playerId: "PlayerC", gameId: "G1", score: 3 },
{ playerId: "PlayerD", gameId: "G1"},
{ playerId: "PlayerE", gameId: "G1", score: null }
]
},
{
$group:
{
_id: "$gameId",
playerId:
{
$top:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": 1 }
}
}
}
}
] )
In this example:在此示例中:
$documents$groupgameId. gameId对文档进行分组。gameId, G1.gameId,G1。PlayerDPlayerE has a null score. PlayerE的分数为空。null。playerId and score fields are specified as output : ["$playerId"," $score"] and returned as array values.playerId和score字段被指定为output : ["$playerId"," $score"],并作为数组值返回。sortBy: { "score": 1 }.sortBy: { "score": 1 }指定排序顺序。PlayerD and PlayerE tied for the top element. PlayerD和PlayerE并列。PlayerD is returned as the top score.PlayerD作为最高分数返回。sortBy.sortBy添加更多字段。[
{
_id: 'G1',
playerId: [ [ 'PlayerD', null ] ]
}
]
$top is not supported as a aggregation expression.不支持作为聚合表达式。
$top is supported as a 支持作为窗口运算符。window operator.
Aggregation pipelines which call 调用$top are subject to the 100 MB limit. $top的聚合管道受100 MB限制。If this limit is exceeded for an individual group, the aggregation fails with an error.如果单个组超过了此限制,则聚合将失败并返回错误。
Consider a 考虑一个包含以下文档的gamescores collection with the following documents:gamescores集合:
db.gamescores.insertMany([
{ playerId: "PlayerA", gameId: "G1", score: 31 },
{ playerId: "PlayerB", gameId: "G1", score: 33 },
{ playerId: "PlayerC", gameId: "G1", score: 99 },
{ playerId: "PlayerD", gameId: "G1", score: 1 },
{ playerId: "PlayerA", gameId: "G2", score: 10 },
{ playerId: "PlayerB", gameId: "G2", score: 14 },
{ playerId: "PlayerC", gameId: "G2", score: 66 },
{ playerId: "PlayerD", gameId: "G2", score: 80 }
])
ScoreYou can use the 您可以使用$top accumulator to find the top score in a single game.$top累加器来查找单个游戏中的最高分数。
db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
playerId:
{
$top:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )
The example pipeline:示例管道:
$match to filter the results on a single gameId. In this case, G1.$match筛选单个gameId上的结果。在这种情况下为G1。$group to group the results by gameId. In this case, G1.$group按gameId对结果进行分组。在这种情况下,G1。$top with output : ["$playerId"," $score"].output : ["$playerId"," $score"]指定$top的输出字段。sortBy: { "score": -1 } to sort the scores in descending order.sortBy: { "score": -1 }按降序对分数进行排序。$top to return the top score in the game.$top返回游戏中的最高分数。The operation returns the following results:该操作返回以下结果:
[ { _id: 'G1', playerId: [ 'PlayerC', 99 ] } ]
Score Across Multiple GamesYou can use the 您可以使用$top accumulator to find the top score in each game.$top累加器查找每场比赛的最高分数。
db.gamescores.aggregate( [
{
$group:
{ _id: "$gameId", playerId:
{
$top:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )
The example pipeline:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$top to return top score for each game.$top返回每场比赛的最高分数。$top with output : ["$playerId", "$score"].output : ["$playerId", "$score"]指定$top的输出字段。sortBy: { "score": -1 } to sort the scores in descending order.sortBy: { "score": -1 }按降序对分数进行排序。The operation returns the following results:该操作返回以下结果:
[
{ _id: 'G2', playerId: [ 'PlayerD', 80 ] },
{ _id: 'G1', playerId: [ 'PlayerC', 99 ] }
]