@@ -30,13 +30,13 @@ Overview
30
30
In this guide, you can learn how to use the {+driver-short+} to perform
31
31
**aggregation operations**.
32
32
33
- Aggregation operations process data in your MongoDB collections and
33
+ You can use aggregation operations to process data in your MongoDB collections and
34
34
return computed results. The MongoDB Aggregation framework, which is
35
- part of the Query API, is modeled on the concept of data processing
36
- pipelines . Documents enter a pipeline that contains one or more stages,
37
- and this pipeline transforms the documents into an aggregated result.
35
+ part of the Query API, is modeled on the concept of a data processing
36
+ pipeline . Documents enter a pipeline that contains one or more stages,
37
+ and each stage transforms the documents to output a final aggregated result.
38
38
39
- An aggregation operation is similar to a car factory. A car factory has
39
+ You can think of an aggregation operation as similar to a car factory. A car factory has
40
40
an assembly line, which contains assembly stations with specialized
41
41
tools to do specific jobs, like drills and welders. Raw parts enter the
42
42
factory, and then the assembly line transforms and assembles them into a
@@ -46,8 +46,8 @@ The **aggregation pipeline** is the assembly line, **aggregation stages** are th
46
46
assembly stations, and **operator expressions** are the
47
47
specialized tools.
48
48
49
- Aggregation Versus Find Operations
50
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
+ Compare Aggregation and Find Operations
50
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
51
52
52
You can use find operations to perform the following actions:
53
53
@@ -66,20 +66,19 @@ You can use aggregation operations to perform the following actions:
66
66
Limitations
67
67
~~~~~~~~~~~
68
68
69
- Keep the following limitations in mind when using aggregation operations:
69
+ The following limitations apply when using aggregation operations:
70
70
71
71
- Returned documents must not violate the
72
72
:manual:`BSON document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>`
73
73
of 16 megabytes.
74
74
- Pipeline stages have a memory limit of 100 megabytes by default. You can exceed this
75
- limit by using the ``allowDiskUse`` method of the
76
- ``AggregateIterable`` type.
75
+ limit by using the ``allowDiskUse()`` method from ``AggregateIterable`` class.
77
76
78
77
.. important:: $graphLookup exception
79
78
80
79
The :manual:`$graphLookup
81
80
</reference/operator/aggregation/graphLookup/>` stage has a strict
82
- memory limit of 100 megabytes and ignores the ``allowDiskUse`` parameter .
81
+ memory limit of 100 megabytes and ignores the ``allowDiskUse`` option .
83
82
84
83
Aggregation Example
85
84
-------------------
@@ -100,18 +99,27 @@ The following {+language+} data class models the documents in this collection:
100
99
Build and Execute an Aggregation Pipeline
101
100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
101
103
- To perform an aggregation, pass a list of aggregation stages to the
104
- ``collection. aggregate()`` method.
102
+ To perform an aggregation on the documents in a collection , pass a list of aggregation
103
+ stages to the `` aggregate()`` method.
105
104
106
- The following code example produces a count of the number of bakeries in each borough
107
- of New York. To do so, it uses an aggregation pipeline with the following stages:
105
+ This example outputs a count of the number of bakeries in each borough
106
+ of New York City. The following code creates aggregation pipeline that contains the
107
+ following stages:
108
108
109
109
- A :manual:`$match </reference/operator/aggregation/match/>` stage to filter for documents
110
- whose ``cuisine`` field contains the value ``"Bakery"``.
110
+ in which the value of the ``cuisine`` field is ``"Bakery"``.
111
111
112
112
- A :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching
113
- documents by the ``borough`` field, accumulating a count of documents for each distinct
114
- value.
113
+ documents by the ``borough`` field, producing a count of documents for each distinct
114
+ value of that field.
115
+
116
+ .. TODO: uncomment when Aggregates Builder page is created
117
+
118
+ .. .. note::
119
+
120
+ .. The following example uses the builders pattern to implement the stages of an
121
+ .. aggregation pipeline. To learn more about how to use the builders pattern, see
122
+ .. :ref:`<aggregates-builders>`
115
123
116
124
.. io-code-block::
117
125
@@ -131,18 +139,23 @@ of New York. To do so, it uses an aggregation pipeline with the following stages
131
139
Document{{_id=Staten Island, count=20}}
132
140
Document{{_id=Missing, count=2}}
133
141
142
+ .. tip::
143
+
144
+ When specifying a group key for the ``$group`` aggregation stage, ensure that you
145
+ escape any ``$`` characters by using the ``\`` character.
146
+
134
147
Explain an Aggregation
135
148
~~~~~~~~~~~~~~~~~~~~~~
136
149
137
150
To view information about how MongoDB executes your operation, you can
138
- instruct MongoDB to ** explain** it . When MongoDB explains an operation, it returns
139
- **execution plans** and performance statistics. An execution
151
+ include the ``$ explain`` aggregation stage in your pipeline . When MongoDB explains an
152
+ operation, it returns **execution plans** and performance statistics. An execution
140
153
plan is a potential way MongoDB can complete an operation.
141
154
When you instruct MongoDB to explain an operation, it returns both the
142
- plan MongoDB executed and any rejected execution plans.
155
+ plan MongoDB selected for the operation and any rejected execution plans.
143
156
144
- The following code example runs the preceding aggregation example and prints the returned
145
- explanation :
157
+ The following code example runs the same aggregation shown in the preceding section
158
+ and adds the ``$explain`` stage to output the operation details :
146
159
147
160
.. io-code-block::
148
161
@@ -156,43 +169,39 @@ explanation:
156
169
:visible: false
157
170
158
171
{
159
- "explain": {
160
- "aggregate": "restaurants",
161
- "pipeline": [
162
- {
163
- "$match": {
164
- "cuisine": "Bakery"
165
- }
166
- },
167
- {
168
- "$group": {
169
- "_id": "$borough",
170
- "count": {
171
- "$sum": 1
172
- }
173
- }
174
- }
175
- ],
176
- "cursor": {}
177
- },
178
- ...
172
+ "explainVersion": "2",
173
+ "queryPlanner": {
174
+ "namespace": "sample_restaurants.restaurants"
175
+ "indexFilterSet": false,
176
+ "parsedQuery": {
177
+ "cuisine": {"$eq": "Bakery"}
178
+ },
179
+ "queryHash": "865F14C3",
180
+ "planCacheKey": "0697561B",
181
+ "optimizedPipeline": true,
182
+ "maxIndexedOrSolutionsReached": false,
183
+ "maxIndexedAndSolutionsReached": false,
184
+ "maxScansToExplodeReached": false,
185
+ "winningPlan": { ... }
186
+ ...
187
+ }
179
188
}
180
189
181
190
Additional Information
182
191
----------------------
183
192
184
193
To view a full list of expression operators, see :manual:`Aggregation
185
- Operators. </reference/operator/aggregation/>`
194
+ Operators </reference/operator/aggregation/>` in the {+mdb-server+} manual.
186
195
187
196
To learn about assembling an aggregation pipeline and view examples, see
188
- :manual:`Aggregation Pipeline. </core/aggregation-pipeline/>`
197
+ :manual:`Aggregation Pipeline </core/aggregation-pipeline/>` in the {+mdb-server+} manual.
189
198
190
199
To learn more about creating pipeline stages, see :manual:`Aggregation
191
- Stages. </reference/operator/aggregation-pipeline/>`
200
+ Stages </reference/operator/aggregation-pipeline/>` in the {+mdb-server+} manual.
192
201
193
202
To learn more about explaining MongoDB operations, see
194
203
:manual:`Explain Output </reference/explain-results/>` and
195
- :manual:`Query Plans. </core/query-plans/>`
204
+ :manual:`Query Plans </core/query-plans/>` in the {+mdb-server+} manual.
196
205
197
206
.. Aggregation Tutorials
198
207
.. ~~~~~~~~~~~~~~~~~~~~~
0 commit comments