On this page本页内容
{
$topN:
{
n: <expression>,
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
n _id value for $group.$group的_id值。$sort.$sort。outputNull和缺少值$topN$topNnull。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:
{
$topN:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": 1 },
n: 3
}
}
}
}
] )
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 }, the null values are sorted to the front of the returned playerId array.sortBy: { "score" : 1 },空值被排序到返回的playerId数组的前面。[
{
_id: 'G1',
playerId: [ [ 'PlayerD', null ], [ 'PlayerE', null ], [ 'PlayerA', 1 ] ]
}
]
When sorting different types, the order of BSON data types is used to determine ordering. 对不同类型排序时,BSON数据类型的顺序用于确定排序。As an example, consider a collection whose values consist of strings and numbers.例如,考虑一个值由字符串和数字组成的集合。
db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: "2" },
{ playerId: "PlayerC", gameId: "G1", score: "" }
]
},
{
$group:
{
_id: "$gameId",
playerId: {
$topN:
{
output: ["$playerId","$score"],
sortBy: {"score": -1},
n: 3
}
}
}
}
] )
In this example:在此示例中:
PlayerAPlayerB"2" score."2"分数。PlayerCBecause the sort is in descending 因为排序是降序{ "score" : -1 }, the string literal values are sorted before PlayerA's numeric score:{ "score" : -1 },所以字符串文字值在PlayerA的数字分数之前排序:
[
{
_id: "G1",
playerId: [ [ "PlayerB", "2" ], [ "PlayerC", "" ], [ "PlayerA", 1 ] ]
}
]
$topN is not supported as a aggregation expression.不支持作为聚合表达式。
$topN is supported as a 支持作为窗口运算符。window operator.
Groups within the $topN aggregation pipeline are subject to the 100 MB limit pipeline limit. $topN聚合管道中的组受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 }
])
ScoresYou can use the 您可以使用$topN accumulator to find the highest scoring players in a single game.$topN累加器查找单个游戏中得分最高的玩家。
db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
playerId:
{
$topN:
{
output: ["$playerId", "$score"],
sortBy: { "score": -1 },
n:3
}
}
}
}
] )
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。{ "score": -1 } to sort the results in descending order.{ "score": -1 }按降序对结果进行排序。$topN with output : ["$playerId"," $score"].output : ["$playerId"," $score"]指定从$topN输出的字段。$topN to return the top three documents with the highest score for the G1 game with n : 3.$topN返回G1游戏n:3中得分最高的前三个文档。The operation returns the following results:该操作返回以下结果:
[
{
_id: 'G1',
playerId: [ [ 'PlayerC', 99 ], [ 'PlayerB', 33 ], [ 'PlayerA', 31 ] ]
}
]
The SQL equivalent to this query is:与此查询等效的SQL是:
SELECT T3.GAMEID,T3.PLAYERID,T3.SCORE FROM GAMESCORES AS GS JOIN (SELECT TOP 3 GAMEID,PLAYERID,SCORE FROM GAMESCORES WHERE GAMEID = 'G1' ORDER BY SCORE DESC) AS T3 ON GS.GAMEID = T3.GAMEID GROUP BY T3.GAMEID,T3.PLAYERID,T3.SCORE ORDER BY T3.SCORE DESC
You can use the 您可以使用$topN accumulator to find the highest scoring players in each game.$topN累加器查找每场比赛中得分最高的玩家。
db.gamescores.aggregate( [
{
$group:
{ _id: "$gameId", playerId:
{
$topN:
{
output: [ "$playerId","$score" ],
sortBy: { "score": -1 },
n: 3
}
}
}
}
] )
The example pipeline:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$topN with output : ["$playerId", "$score"].output : ["$playerId", "$score"]指定从$topN输出的字段。{ "score": -1 } to sort the results in descending order.{ "score": -1 }按降序对结果进行排序。$topN to return the top three documents with the highest score for each game with n: 3.$topN返回n:3的每个游戏得分最高的前三个文档。The operation returns the following results:该操作返回以下结果:
[
{
_id: 'G1',
playerId: [ [ 'PlayerC', 99 ], [ 'PlayerB', 33 ], [ 'PlayerA', 31 ] ]
},
{
_id: 'G2',
playerId: [ [ 'PlayerD', 80 ], [ 'PlayerC', 66 ], [ 'PlayerB', 14 ] ]
}
]
The SQL equivalent to this query is:与此查询等效的SQL是:
SELECT PLAYERID,GAMEID,SCORE FROM( SELECT ROW_NUMBER() OVER (PARTITION BY GAMEID ORDER BY SCORE DESC) AS GAMERANK, GAMEID,PLAYERID,SCORE FROM GAMESCORES ) AS T WHERE GAMERANK <= 3 ORDER BY GAMEID
n Based on the Group Key for $group$Group的组密钥计算nYou can also assign the value of 还可以动态指定n dynamically. n的值。In this example, the 在本例中,$cond expression is used on the gameId field.$cond表达式用于gameId字段。
db.gamescores.aggregate([
{
$group:
{
_id: {"gameId": "$gameId"},
gamescores:
{
$topN:
{
output: "$score",
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } },
sortBy: { "score": -1 }
}
}
}
}
] )
The example pipeline:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$topN with output : "$score".$topN输出的字段,其输出为output : "$score"。gameId is G2 then n is 1, otherwise n is 3.gameId为G2,则n为1,否则n为3。{ "score": -1 } to sort the results in descending order.{ "score": -1 }按降序对结果进行排序。The operation returns the following results:该操作返回以下结果:
[
{ _id: { gameId: 'G1' }, gamescores: [ 99, 33, 31 ] },
{ _id: { gameId: 'G2' }, gamescores: [ 80 ] }
]