|
| 1 | +.. _rust-aggregation: |
| 2 | + |
| 3 | +=========== |
| 4 | +Aggregation |
| 5 | +=========== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to perform **aggregation operations** in |
| 17 | +the {+driver-short+}. |
| 18 | + |
| 19 | +Aggregation operations process data in your MongoDB collections based on |
| 20 | +specifications you can set in an **aggregation pipeline**. An aggregation |
| 21 | +pipeline consists of one or more **stages**. Each stage performs an |
| 22 | +operation based on its expression operators. After the driver executes |
| 23 | +the aggregation pipeline, it returns an aggregated result. |
| 24 | + |
| 25 | +Analogy |
| 26 | +~~~~~~~ |
| 27 | + |
| 28 | +Aggregation operations function similarly to car factories with assembly |
| 29 | +lines. The assembly lines have stations with specialized tools to |
| 30 | +perform specific tasks. For example, when building a car, the assembly |
| 31 | +line begins with the frame. Then, as the car frame moves through the |
| 32 | +assembly line, each station assembles a separate part. The result is a |
| 33 | +transformed final product, the finished car. |
| 34 | + |
| 35 | +The assembly line represents the *aggregation pipeline*, the individual |
| 36 | +stations represent the *aggregation stages*, the specialized tools |
| 37 | +represent the *expression operators*, and the finished product |
| 38 | +represents the *aggregated result*. |
| 39 | + |
| 40 | +Compare Aggregation and Find Operations |
| 41 | +--------------------------------------- |
| 42 | + |
| 43 | +The following table lists the different tasks you can perform with find |
| 44 | +operations, compared to what you can achieve with aggregation |
| 45 | +operations. The aggregation framework provides expanded functionality |
| 46 | +that allows you to transform and manipulate your data. |
| 47 | + |
| 48 | +.. list-table:: |
| 49 | + :header-rows: 1 |
| 50 | + :widths: 50 50 |
| 51 | + |
| 52 | + * - Find Operations |
| 53 | + - Aggregation Operations |
| 54 | + |
| 55 | + * - | Select *certain* documents to return |
| 56 | + | Select *which* fields to return |
| 57 | + | Sort the results |
| 58 | + | Limit the results |
| 59 | + | Count the results |
| 60 | + - | Select *certain* documents to return |
| 61 | + | Select *which* fields to return |
| 62 | + | Sort the results |
| 63 | + | Limit the results |
| 64 | + | Count the results |
| 65 | + | Rename fields |
| 66 | + | Compute new fields |
| 67 | + | Summarize data |
| 68 | + | Connect and merge data sets |
| 69 | + |
| 70 | +Limitations |
| 71 | +----------- |
| 72 | + |
| 73 | +When performing aggregation operations, consider the following |
| 74 | +limitations: |
| 75 | + |
| 76 | +- Returned documents must not violate the :manual:`BSON document size |
| 77 | + limit </reference/limits/#BSON-Document-Size>` of 16 megabytes. |
| 78 | +- Pipeline stages have a memory limit of 100 megabytes by default. If |
| 79 | + required, you may exceed this limit by setting the `allow_disk_use |
| 80 | + <{+api+}/options/struct.AggregateOptions.html#structfield.allow_disk_use>`__ |
| 81 | + field in your ``AggregateOptions``. |
| 82 | +- The :manual:`$graphLookup |
| 83 | + </reference/operator/aggregation/graphLookup/>` operator |
| 84 | + has a strict memory limit of 100 megabytes and ignores |
| 85 | + the ``allow_disk_use`` setting. |
| 86 | + |
| 87 | +Examples |
| 88 | +-------- |
| 89 | + |
| 90 | +.. TODO decide if we should use structs on this page. might get long |
| 91 | + |
| 92 | +To run the examples in this section, load the sample data into a |
| 93 | +collection called ``db.site_users`` with the following code: |
| 94 | + |
| 95 | +.. literalinclude:: /includes/fundamentals/code-snippets/aggregation.rs |
| 96 | + :start-after: begin-insert |
| 97 | + :end-before: end-insert |
| 98 | + :language: rust |
| 99 | + :dedent: |
| 100 | + |
| 101 | +.. include:: /includes/fundamentals/automatic-creation.rst |
| 102 | + |
| 103 | +Each document represents a user of a book-reviewing website and contains |
| 104 | +information about their name, age, genre interests, and date that they were |
| 105 | +last active on the website. |
| 106 | + |
| 107 | +Age Insights by Genre |
| 108 | +~~~~~~~~~~~~~~~~~~~~~ |
| 109 | + |
| 110 | +The following example calculates the average, minimum, and maximum age of users |
| 111 | +interested in each genre. |
| 112 | + |
| 113 | +The aggregation pipeline contains the following stages: |
| 114 | + |
| 115 | +- An ``$unwind`` stage to separate each array entry in the |
| 116 | + ``genre_interests`` field into a new document. |
| 117 | +- A ``$group`` stage to group documents by the value of the |
| 118 | + ``genre_interests`` field. This stage finds the average, minimum, and |
| 119 | + maximum user age for users, using the ``$avg``, ``$min``, and ``$max`` |
| 120 | + operators, respectively. |
| 121 | + |
| 122 | +.. io-code-block:: |
| 123 | + |
| 124 | + .. input:: /includes/fundamentals/code-snippets/aggregation.rs |
| 125 | + :start-after: begin-age-agg |
| 126 | + :end-before: end-age-agg |
| 127 | + :language: rust |
| 128 | + :dedent: |
| 129 | + |
| 130 | + .. output:: |
| 131 | + :language: console |
| 132 | + :visible: false |
| 133 | + |
| 134 | + * { "_id": "memoir", "avg_age": 25.8, "min_age": 18, "max_age": 39 } |
| 135 | + * { "_id": "sci-fi", "avg_age": 42, "min_age": 18, "max_age": 66 } |
| 136 | + * { "_id": "fiction", "avg_age": 33.333333333333336, "min_age": 16, "max_age": 66 } |
| 137 | + * { "_id": "nonfiction", "avg_age": 53.5, "min_age": 31, "max_age": 76 } |
| 138 | + * { "_id": "self help", "avg_age": 56, "min_age": 56, "max_age": 56 } |
| 139 | + * { "_id": "poetry", "avg_age": 39, "min_age": 39, "max_age": 39 } |
| 140 | + * { "_id": "literary", "avg_age": 49.5, "min_age": 21, "max_age": 76 } |
| 141 | + * { "_id": "fantasy", "avg_age": 34.666666666666664, "min_age": 18, "max_age": 66 } |
| 142 | + * { "_id": "mystery", "avg_age": 24.666666666666668, "min_age": 20, "max_age": 31 } |
| 143 | + * { "_id": "theory", "avg_age": 33, "min_age": 21, "max_age": 45 } |
| 144 | + * { "_id": "art", "avg_age": 39, "min_age": 39, "max_age": 39 } |
| 145 | + * { "_id": "sports", "avg_age": 22.5, "min_age": 16, "max_age": 29 } |
| 146 | + |
| 147 | +Group by Time Component |
| 148 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 149 | + |
| 150 | +The following example finds how many users were last active in each |
| 151 | +month. |
| 152 | + |
| 153 | +The aggregation pipeline contains the following stages: |
| 154 | + |
| 155 | +- A ``$project`` stage to extract the month from the ``last_active`` |
| 156 | + field as a number into the ``month_last_active`` field. |
| 157 | +- A ``$group`` stage to group documents by the ``month_last_active`` |
| 158 | + field and count the number of documents for each month. |
| 159 | +- A ``$sort`` stage to set an ascending sort on the month. |
| 160 | + |
| 161 | +.. io-code-block:: |
| 162 | + |
| 163 | + .. input:: /includes/fundamentals/code-snippets/aggregation.rs |
| 164 | + :start-after: begin-lastactive-agg |
| 165 | + :end-before: end-lastactive-agg |
| 166 | + :language: rust |
| 167 | + :dedent: |
| 168 | + |
| 169 | + .. output:: |
| 170 | + :language: console |
| 171 | + :visible: false |
| 172 | + |
| 173 | + * { "_id": { "month_last_active": 1 }, "number": 3 } |
| 174 | + * { "_id": { "month_last_active": 5 }, "number": 4 } |
| 175 | + * { "_id": { "month_last_active": 6 }, "number": 1 } |
| 176 | + * { "_id": { "month_last_active": 7 }, "number": 1 } |
| 177 | + * { "_id": { "month_last_active": 8 }, "number": 2 } |
| 178 | + * { "_id": { "month_last_active": 11 }, "number": 1 } |
| 179 | + |
| 180 | +Calculate Popular Genres |
| 181 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 182 | + |
| 183 | +The following example finds the three most popular genres based on how |
| 184 | +often they appear in users' interests. |
| 185 | + |
| 186 | +The aggregation pipeline contains the following stages: |
| 187 | + |
| 188 | +- An ``$unwind`` stage to separate each array entry in the |
| 189 | + ``genre_interests`` field into a new document. |
| 190 | +- A ``$group`` stage to group documents by the ``genre_interests`` |
| 191 | + field and count the number of documents for each genre. |
| 192 | +- A ``$sort`` stage to set a descending sort on the genre popularity. |
| 193 | +- A ``$limit`` stage to show only the first three genres. |
| 194 | + |
| 195 | +.. io-code-block:: |
| 196 | + |
| 197 | + .. input:: /includes/fundamentals/code-snippets/aggregation.rs |
| 198 | + :start-after: begin-popular-agg |
| 199 | + :end-before: end-popular-agg |
| 200 | + :language: rust |
| 201 | + :dedent: |
| 202 | + |
| 203 | + .. output:: |
| 204 | + :language: console |
| 205 | + :visible: false |
| 206 | + |
| 207 | + * { "_id": "fiction", "number": 6 } |
| 208 | + * { "_id": "memoir", "number": 5 } |
| 209 | + * { "_id": "literary", "number": 4 } |
| 210 | + |
| 211 | +Additional Information |
| 212 | +---------------------- |
| 213 | + |
| 214 | +To learn more about the terms mentioned, see the following |
| 215 | +guides: |
| 216 | + |
| 217 | +- :manual:`Expression Operators </reference/operator/aggregation/>` |
| 218 | +- :manual:`Aggregation Pipeline </core/aggregation-pipeline/>` |
| 219 | +- :manual:`Aggregation Stages </meta/aggregation-quick-reference/#stages>` |
| 220 | +- :manual:`Operator Expressions </meta/aggregation-quick-reference/#operator-expressions>` |
| 221 | +- :manual:`Aggregation Pipeline Limits </core/aggregation-pipeline-limits/>` |
| 222 | + |
| 223 | +.. TODO To view more aggregation examples, see the following guides: |
| 224 | +.. |
| 225 | +.. - :ref:`Count <>` |
| 226 | +.. - :ref:`Limit <>` |
| 227 | +.. - :ref:`Skip <>` |
| 228 | +.. - :ref:`Text <>` |
| 229 | + |
| 230 | +.. TODO To learn more about the ``aggregate()`` method and its behavior, see |
| 231 | +.. :ref:`Retrieve Data <>`. |
| 232 | + |
| 233 | +API Documentation |
| 234 | +~~~~~~~~~~~~~~~~~ |
| 235 | + |
| 236 | +To learn more about any of the methods or types discussed in this |
| 237 | +guide, see the following API Documentation: |
| 238 | + |
| 239 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
| 240 | +- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__ |
0 commit comments