On this page本页内容
{
$bottom:
{
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
| sortBy | $sort.$sort。
| |
| output |
Consider the following aggregation that returns the bottom document from a group of scores:考虑以下从一组分数中返回底部文档的聚合:
$bottom$bottomnull。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:
{
$bottom:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )
In this example:在这个例子中:
$documents$groupgameId. gameId对文档进行分组。gameId, G1.gameId,G1。PlayerDPlayerE has a null score. PlayerE的score为空。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 bottom element. PlayerD is returned as the bottom score.PlayerD和PlayerE为底部元素并列。返回PlayerD作为最低score。sortBy添加更多字段。[
{
_id: 'G1',
playerId: [ [ 'PlayerD', null ] ]
}
]
$bottom is not supported as a aggregation expression.不支持作为聚合表达式。
$bottom is supported as a 支持作为窗口运算符。window operator.
Aggregation pipelines which call 调用$bottom are subject to the 100 MB limit. $bottom的聚合管道受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 您可以使用$bottom accumulator to find the bottom score in a single game.$bottom累加器查找单个游戏中的最低分数。
db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
playerId:
{
$bottom:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )
The example pipeline:示例管道:
$match to filter the results on a single gameId. $match筛选单个gameId的结果。G1.G1。$group to group the results by gameId. $group按gameId对结果进行分组。G1.G1。$bottom with output : ["$playerId"," $score"].$bottom输出的字段,output : ["$playerId"," $score"]。sortBy: { "score": -1 } to sort the scores in descending order.sortBy: { "score": -1 }按降序对分数进行排序。$bottom to return the bottom score for the game.$bottom返回游戏的最低分数。The operation returns the following results:该操作返回以下结果:
[ { _id: 'G1', playerId: [ 'PlayerD', 1 ] } ]
Score Across Multiple GamesScoreYou can use the 您可以使用$bottom accumulator to find the bottom score in each game.$bottom累加器查找每场游戏的最低Score。
db.gamescores.aggregate( [
{
$group:
{ _id: "$gameId", playerId:
{
$bottom:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 }
}
}
}
}
] )
The example pipeline:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$bottom to return the bottom score for each game.$bottom返回每个游戏的最低score。$bottom with output : ["$playerId", "$score"].$bottom输出的字段,output : ["$playerId", "$score"]。sortBy: { "score": -1 } to sort the scores in descending order.sortBy: { "score": -1 }按降序对分数进行排序。The operation returns the following results:该操作返回以下结果:
[
{ _id: 'G2', playerId: [ 'PlayerA', 10 ] },
{ _id: 'G1', playerId: [ 'PlayerD', 1 ] }
]