|
| 1 | +================================= |
| 2 | +$bottom (aggregation accumulator) |
| 3 | +================================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. group:: $bottom |
| 17 | + |
| 18 | + .. versionadded:: 5.2 |
| 19 | + |
| 20 | + Returns the bottom element within a group according to the specified |
| 21 | + sort order. |
| 22 | + |
| 23 | +Syntax |
| 24 | +------ |
| 25 | + |
| 26 | +.. code-block:: none |
| 27 | + :copyable: false |
| 28 | + |
| 29 | + { |
| 30 | + $bottom: |
| 31 | + { |
| 32 | + sortBy: { <field1>: <sort order>, <field2>: <sort order> ... }, |
| 33 | + output: <expression> |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | +.. list-table:: |
| 38 | + :header-rows: 1 |
| 39 | + :widths: 15 15 70 |
| 40 | + |
| 41 | + * - Field |
| 42 | + - Necessity |
| 43 | + - Description |
| 44 | + |
| 45 | + * - sortBy |
| 46 | + |
| 47 | + - Required |
| 48 | + |
| 49 | + - Specifies the order of results, with syntax similar to |
| 50 | + :pipeline:`$sort`. |
| 51 | + |
| 52 | + * - output |
| 53 | + |
| 54 | + - Required |
| 55 | + |
| 56 | + - Represents the output for each element in the group |
| 57 | + and can be any expression. |
| 58 | + |
| 59 | +Behavior |
| 60 | +-------- |
| 61 | + |
| 62 | +Null and Missing Values |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +Consider the following aggregation that returns the bottom |
| 66 | +document from a group of scores: |
| 67 | + |
| 68 | +- ``$bottom`` does not filter out null values. |
| 69 | +- ``$bottom`` converts missing values to null. |
| 70 | + |
| 71 | +.. code-block:: javascript |
| 72 | + :emphasize-lines: 7,8 |
| 73 | + |
| 74 | + db.aggregate( [ |
| 75 | + { |
| 76 | + $documents: [ |
| 77 | + { playerId: "PlayerA", gameId: "G1", score: 1 }, |
| 78 | + { playerId: "PlayerB", gameId: "G1", score: 2 }, |
| 79 | + { playerId: "PlayerC", gameId: "G1", score: 3 }, |
| 80 | + { playerId: "PlayerD", gameId: "G1"}, |
| 81 | + { playerId: "PlayerE", gameId: "G1", score: null } |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + $group: |
| 86 | + { |
| 87 | + _id: "$gameId", |
| 88 | + playerId: |
| 89 | + { |
| 90 | + $bottom: |
| 91 | + { |
| 92 | + output: [ "$playerId", "$score" ], |
| 93 | + sortBy: { "score": -1 } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] ) |
| 99 | + |
| 100 | +In this example: |
| 101 | + |
| 102 | +- :pipeline:`$documents` creates the literal documents that contain |
| 103 | + player scores. |
| 104 | +- :pipeline:`$group` groups the documents by ``gameId``. This |
| 105 | + example has only one ``gameId``, ``G1``. |
| 106 | +- ``PlayerD`` has a missing score and ``PlayerE`` has a |
| 107 | + null ``score``. These values are both considered as null. |
| 108 | +- The ``playerId`` and ``score`` fields are specified as |
| 109 | + ``output : ["$playerId"," $score"]`` and returned as array values. |
| 110 | +- Specify the sort order with ``sortBy: { "score": -1 }``. |
| 111 | +- ``PlayerD`` and ``PlayerE`` tied for the bottom element. ``PlayerD`` |
| 112 | + is returned as the bottom ``score``. |
| 113 | +- To have more deterministic tie breaking behavior for multiple null |
| 114 | + values, add more fields to``sortBy``. |
| 115 | + |
| 116 | +.. code-block:: javascript |
| 117 | + :copyable: false |
| 118 | + |
| 119 | + [ |
| 120 | + { |
| 121 | + _id: 'G1', |
| 122 | + playerId: [ [ 'PlayerD', null ] ] |
| 123 | + } |
| 124 | + ] |
| 125 | + |
| 126 | +Restrictions |
| 127 | +------------ |
| 128 | + |
| 129 | +Window Function and Aggregation Expression Support |
| 130 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +``$bottom`` is not supported as a |
| 133 | +:ref:`aggregation expression <aggregation-expressions>`. |
| 134 | + |
| 135 | +``$bottom`` is supported as a |
| 136 | +:pipeline:`window operator <$setWindowFields>`. |
| 137 | + |
| 138 | +Memory Limit Considerations |
| 139 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 140 | + |
| 141 | +Aggregation pipelines which call ``$bottom`` are subject to the |
| 142 | +:ref:`100 MB limit <agg-memory-restrictions>`. If this |
| 143 | +limit is exceeded for an individual group, the aggregation fails |
| 144 | +with an error. |
| 145 | + |
| 146 | +Examples |
| 147 | +-------- |
| 148 | + |
| 149 | +Consider a ``gamescores`` collection with the following documents: |
| 150 | + |
| 151 | +.. code-block:: javascript |
| 152 | + |
| 153 | + db.gamescores.insertMany([ |
| 154 | + { playerId: "PlayerA", gameId: "G1", score: 31 }, |
| 155 | + { playerId: "PlayerB", gameId: "G1", score: 33 }, |
| 156 | + { playerId: "PlayerC", gameId: "G1", score: 99 }, |
| 157 | + { playerId: "PlayerD", gameId: "G1", score: 1 }, |
| 158 | + { playerId: "PlayerA", gameId: "G2", score: 10 }, |
| 159 | + { playerId: "PlayerB", gameId: "G2", score: 14 }, |
| 160 | + { playerId: "PlayerC", gameId: "G2", score: 66 }, |
| 161 | + { playerId: "PlayerD", gameId: "G2", score: 80 } |
| 162 | + ]) |
| 163 | + |
| 164 | +Find the Bottom ``Score`` |
| 165 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 166 | + |
| 167 | +You can use the ``$bottom`` accumulator to find the bottom score in a |
| 168 | +single game. |
| 169 | + |
| 170 | +.. code-block:: javascript |
| 171 | + |
| 172 | + db.gamescores.aggregate( [ |
| 173 | + { |
| 174 | + $match : { gameId : "G1" } |
| 175 | + }, |
| 176 | + { |
| 177 | + $group: |
| 178 | + { |
| 179 | + _id: "$gameId", |
| 180 | + playerId: |
| 181 | + { |
| 182 | + $bottom: |
| 183 | + { |
| 184 | + output: [ "$playerId", "$score" ], |
| 185 | + sortBy: { "score": -1 } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + ] ) |
| 191 | + |
| 192 | +The example pipeline: |
| 193 | + |
| 194 | +- Uses :pipeline:`$match` to filter the results on a single ``gameId``. |
| 195 | + In this case, ``G1``. |
| 196 | +- Uses :pipeline:`$group` to group the results by ``gameId``. In this |
| 197 | + case, ``G1``. |
| 198 | +- Specifies the fields that are output for ``$bottom`` with |
| 199 | + ``output : ["$playerId"," $score"]``. |
| 200 | +- Uses ``sortBy: { "score": -1 }`` to sort the scores in descending order. |
| 201 | +- Uses ``$bottom`` to return the bottom score for the game. |
| 202 | + |
| 203 | +The operation returns the following results: |
| 204 | + |
| 205 | +.. code-block:: javascript |
| 206 | + :copyable: false |
| 207 | + |
| 208 | + [ { _id: 'G1', playerId: [ 'PlayerD', 1 ] } ] |
| 209 | + |
| 210 | +Finding the Bottom ``Score`` Across Multiple Games |
| 211 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 212 | + |
| 213 | +You can use the ``$bottom`` accumulator to find the bottom ``score`` |
| 214 | +in each game. |
| 215 | + |
| 216 | +.. code-block:: javascript |
| 217 | + |
| 218 | + db.gamescores.aggregate( [ |
| 219 | + { |
| 220 | + $group: |
| 221 | + { _id: "$gameId", playerId: |
| 222 | + { |
| 223 | + $bottom: |
| 224 | + { |
| 225 | + output: [ "$playerId", "$score" ], |
| 226 | + sortBy: { "score": -1 } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + ] ) |
| 232 | + |
| 233 | +The example pipeline: |
| 234 | + |
| 235 | +- Uses ``$group`` to group the results by ``gameId``. |
| 236 | +- Uses ``$bottom`` to return the bottom ``score`` for each game. |
| 237 | +- Specifies the fields that are output for ``$bottom`` with |
| 238 | + ``output : ["$playerId", "$score"]``. |
| 239 | +- Uses ``sortBy: { "score": -1 }`` to sort the scores in descending order. |
| 240 | + |
| 241 | +The operation returns the following results: |
| 242 | + |
| 243 | +.. code-block:: javascript |
| 244 | + :copyable: false |
| 245 | + |
| 246 | + [ |
| 247 | + { _id: 'G2', playerId: [ 'PlayerA', 10 ] }, |
| 248 | + { _id: 'G1', playerId: [ 'PlayerD', 1 ] } |
| 249 | + ] |
0 commit comments