$bottom (aggregation accumulator)

On this page本页内容

Definition定义

$bottom

New in version 5.2.在版本5.2中新增

Returns the bottom element within a group according to the specified sort order.根据指定的排序顺序返回组中的底部元素。

Syntax语法

{
   $bottom:
      {
         sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
         output: <expression>
      }
}
Field字段Necessity必要性Description描述
sortByRequired必需Specifies the order of results, with syntax similar to $sort.指定结果的顺序,语法类似于$sort
outputRequired必需Represents the output for each element in the group and can be any expression.表示组中每个元素的输出,可以是任何表达式。

Behavior行为

Null and Missing Values空值和缺失值

Consider the following aggregation that returns the bottom document from a group of scores:考虑以下从一组分数中返回底部文档的聚合:

  • $bottom does not filter out null values.不筛选空值。
  • $bottom 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:
            {
               $bottom:
                  {
                     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.这个例子只有一个gameIdG1
  • PlayerD has a missing score and PlayerE has a null score. 缺少分数,PlayerEscore为空。These values are both considered as null.这些值都被视为空。
  • The playerId and score fields are specified as output : ["$playerId"," $score"] and returned as array values.playerIdscore字段被指定为output : ["$playerId"," $score"],并作为数组值返回。
  • Specify the sort order with sortBy: { "score": -1 }.使用sortBy: { "score": -1 }指定排序顺序。
  • PlayerD and PlayerE tied for the bottom element. PlayerD is returned as the bottom score.PlayerDPlayerE为底部元素并列。返回PlayerD作为最低score
  • To have more deterministic tie breaking behavior for multiple null values, add more fields to``sortBy``.要对多个空值具有更确定的平局打破行为,请向sortBy添加更多字段。
[
   {
      _id: 'G1',
      playerId: [ [ 'PlayerD', null ] ]
   }
]

Restrictions限制

Window Function and Aggregation Expression Support窗口函数和聚合表达式支持

$bottom is not supported as a aggregation expression.不支持作为聚合表达式

$bottom is supported as a window operator.支持作为窗口运算符

Memory Limit Considerations内存限制注意事项

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.如果单个组超过此限制,则聚合失败并出现错误。

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 Bottom 找到最低Score

You 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:示例管道:

  • Uses $match to filter the results on a single gameId. 使用$match筛选单个gameId的结果。In this case, G1.在这种情况下为G1
  • Uses $group to group the results by gameId. 使用$groupgameId对结果进行分组。In this case, G1.在这种情况下为G1
  • Specifies the fields that are output for $bottom with output : ["$playerId"," $score"].指定为$bottom输出的字段,output : ["$playerId"," $score"]
  • Uses sortBy: { "score": -1 } to sort the scores in descending order.使用sortBy: { "score": -1 }按降序对分数进行排序。
  • Uses $bottom to return the bottom score for the game.使用$bottom返回游戏的最低分数。

The operation returns the following results:该操作返回以下结果:

[ { _id: 'G1', playerId: [ 'PlayerD', 1 ] } ]

Finding the Bottom Score Across Multiple Games在多个游戏中找到最低Score

You 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:示例管道:

  • Uses $group to group the results by gameId.使用$groupgameId对结果进行分组。
  • Uses $bottom to return the bottom score for each game.使用$bottom返回每个游戏的最低score
  • Specifies the fields that are output for $bottom with output : ["$playerId", "$score"].指定为$bottom输出的字段,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: [ 'PlayerA', 10 ] },
   { _id: 'G1', playerId: [ 'PlayerD', 1 ] }
]
←  $binarySize (aggregation)$bottomN (aggregation accumulator) →