$top (aggregation accumulator)
On this page本页内容
Definition定义
Syntax语法
{
$top:
{
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
sortBy | $sort .$sort 。 | |
output |
Behavior行为
Null and Missing ValuesNull
值和缺失值
Consider the following aggregation that returns the top document from a group of scores:考虑以下聚合,该聚合从一组分数中返回排名靠前的文档:
$top
does not filter out null values.不筛选空值。$top
converts missing values to null.将缺少的值转换为null
。
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
creates the literal documents that contain player scores.创建包含玩家分数的文字文档。$group
groups the documents by按gameId
.gameId
对文档进行分组。This example has only one这个例子只有一个gameId
,G1
.gameId
,G1
。PlayerD
has a missing score andPlayerE
has a nullscore
.PlayerD
的score
缺失,PlayerE
的score
为空。These values are both considered as null.这两个值都被视为null
。TheplayerId
andscore
fields are specified asoutput : ["$playerId"," $score"]
and returned as array values.playerId
和score
字段被指定为output : ["$playerId"," $score"]
,并作为数组值返回。Specify the sort order with使用sortBy: { "score": 1 }
.sortBy: { "score": 1 }
指定排序顺序。PlayerD
andPlayerE
tied for the top element.PlayerD
和PlayerE
并列第一。PlayerD
is returned as the topscore
.PlayerD
作为最高分数返回。To have more deterministic tie breaking behavior for multiple null values, add more fields to``sortBy``.若要为多个null
值提供更具确定性的平局打破行为,请在sortBy
中添加更多字段。
[
{
_id: 'G1',
playerId: [ [ 'PlayerD', null ] ]
}
]
Restrictions限制
Window Function and Aggregation Expression Support窗口函数和聚合表达式支持
$top
is not supported as a aggregation expression.$top
不支持作为聚合表达式。
$top
is supported as a window operator
.$top
被支持作为窗口运算符。
Memory Limit Considerations内存限制注意事项
Aggregation pipelines which call 调用$top
are subject to the 100 MB limit. $top
的聚合管道受100MB限制。If this limit is exceeded for an individual group, the aggregation fails with an error.如果单个组超过此限制,则聚合将失败并出现错误。
Examples实例
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 }
])
Find the Top Score
查找最高分
Score
You 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:示例管道:
Uses使用$match
to filter the results on a singlegameId
. In this case,G1
.$match
筛选单个gameId
的结果。在这种情况下,G1
。Uses使用$group
to group the results bygameId
. In this case,G1
.$group
按gameId
对结果进行分组。在这种情况下,G1
。Specifies the fields that are output for指定$top
withoutput : ["$playerId"," $score"]
.$top
的输出字段,使用output : ["$playerId"," $score"]
。Uses使用sortBy: { "score": -1 }
to sort the scores in descending order.sortBy: { "score": -1 }
按降序对分数进行排序。Uses使用$top
to return the top score in the game.$top
返回游戏中的最高分数。
The operation returns the following results:该操作返回以下结果:
[ { _id: 'G1', playerId: [ 'PlayerC', 99 ] } ]
Find the Top Score
Across Multiple Games在多个游戏中查找最高分
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:示例管道:
Uses使用$group
to group the results bygameId
.$group
按gameId
对结果进行分组。Uses使用$top
to return topscore
for each game.$top
返回每场比赛的最高分数。Specifies the fields that are output for指定$top
withoutput : ["$playerId", "$score"]
.$top
的输出字段,使用output : ["$playerId", "$score"]
。Uses使用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 ] }
]