1
+ .. _java-aggregation:
2
+
1
3
===========
2
4
Aggregation
3
5
===========
4
6
7
+ .. facet::
8
+ :name: genre
9
+ :values: reference
10
+
11
+ .. meta::
12
+ :keywords: code example, transform, computed
13
+
5
14
.. contents:: On this page
6
15
:local:
7
16
:backlinks: none
@@ -11,39 +20,41 @@ Aggregation
11
20
Overview
12
21
--------
13
22
14
- .. _java-aggregation:
15
-
16
- In this guide, you can learn how to use **aggregation operations** in the MongoDB Java driver.
23
+ In this guide, you can learn how to use the {+driver-short+} to perform
24
+ **aggregation operations**.
17
25
18
26
Aggregation operations process data in your MongoDB collections and
19
- return computed results. The MongoDB Aggregation framework, part of the
20
- Query API, is modeled on the concept of data processing pipelines.
21
- Documents enter a pipeline comprised of one or more stages that
22
- transforms the documents into an aggregated result.
27
+ return computed results. The MongoDB Aggregation framework, which is
28
+ part of the Query API, is modeled on the concept of data processing
29
+ pipelines. Documents enter a pipeline comprised of one or more stages,
30
+ and this pipeline transforms the documents into an aggregated result.
23
31
24
- Another way to think of aggregation is like a car factory. Within the car factory is an assembly line, along which
25
- are assembly stations with specialized tools to do a specific job, like drills and welders. Raw parts enter the factory,
26
- which are then transformed and assembled into a finished product.
32
+ An aggregation operation is similar to a car factory. A car factory has
33
+ an assembly line, which contains assembly stations with specialized
34
+ tools to do specific jobs, like drills and welders. Raw parts enter the
35
+ factory, and then the assembly line transforms and assembles them into a
36
+ finished product.
27
37
28
- The **aggregation pipeline** is the assembly line, **aggregation stages** are the assembly stations, and
29
- **operator expressions** are the specialized tools.
38
+ The **aggregation pipeline** is the assembly line, **aggregation
39
+ stages** are the assembly stations, and **operator expressions** are the
40
+ specialized tools.
30
41
31
- Aggregation and Find Operations Compared
32
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
+ Compare Aggregation and Find Operations
43
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
44
34
- Using `` find`` operations, you can :
45
+ You can use find operations to perform the following actions :
35
46
36
- - select *what* documents to return
37
- - select *what* fields to return
38
- - sort the results
47
+ - Select *what* documents to return
48
+ - Select *what* fields to return
49
+ - Sort the results
39
50
40
- Using `` aggregation`` operations, you can :
51
+ You can use aggregation operations to perform the following actions :
41
52
42
- - perform all `` find`` operations
43
- - rename fields
44
- - calculate fields
45
- - summarize data
46
- - group values
53
+ - Perform find operations
54
+ - Rename fields
55
+ - Calculate fields
56
+ - Summarize data
57
+ - Group values
47
58
48
59
Aggregation operations have some :manual:`limitations </core/aggregation-pipeline-limits/>` you must keep in mind:
49
60
@@ -70,8 +81,8 @@ Useful References
70
81
Runnable Examples
71
82
-----------------
72
83
73
- Base Setup
74
- ~~~~~~~~~~
84
+ Import Classes
85
+ ~~~~~~~~~~~~~~
75
86
76
87
Create a new Java file called ``AggTour.java`` and include the following import statements:
77
88
@@ -81,7 +92,6 @@ Create a new Java file called ``AggTour.java`` and include the following import
81
92
:start-after: begin imports
82
93
:end-before: end imports
83
94
84
-
85
95
Connect to a MongoDB Deployment
86
96
+++++++++++++++++++++++++++++++
87
97
@@ -91,22 +101,23 @@ Connect to a MongoDB Deployment
91
101
92
102
public static void main(String[] args) {
93
103
// Replace the uri string with your MongoDB deployment's connection string
94
- String uri = "<connection string uri >";
104
+ String uri = "<connection string>";
95
105
96
106
MongoClient mongoClient = MongoClients.create(uri);
97
107
MongoDatabase database = mongoClient.getDatabase("aggregation");
98
108
MongoCollection<Document> collection = database.getCollection("restaurants");
99
109
100
- // aggregation here
110
+ // Paste the aggregation code here
101
111
}
102
112
}
103
113
104
- .. seealso ::
114
+ .. tip ::
105
115
106
- For information on connecting to MongoDB, see the :ref:`Connection Guide <mongoclient>`
116
+ To learn more about connecting to MongoDB, see the :ref:`Connection
117
+ Guide <connect-to-mongodb>`.
107
118
108
- Insert the Data
109
- +++++++++++++++
119
+ Insert Sample Data
120
+ ++++++++++++++++++
110
121
111
122
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
112
123
:language: java
@@ -133,15 +144,15 @@ In the following example, the aggregation pipeline:
133
144
- Uses a :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching documents by the ``stars``
134
145
field, accumulating a count of documents for each distinct value of ``stars``.
135
146
136
- .. seealso ::
147
+ .. note ::
137
148
138
149
You can build the expressions used in this example using the :ref:`aggregation builders <aggregates-builders>`.
139
150
140
151
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
141
152
:language: java
142
153
:dedent:
143
- :start-after: begin aggregation one
144
- :end-before: end aggregation one
154
+ :start-after: begin aggregation basic
155
+ :end-before: end aggregation basic
145
156
146
157
The preceding aggregation should produce the following results:
147
158
@@ -164,32 +175,45 @@ To view information about how MongoDB executes your operation, use the
164
175
``explain()`` method of the ``AggregateIterable`` class. The ``explain()``
165
176
method returns **execution plans** and performance statistics. An execution
166
177
plan is a potential way MongoDB can complete an operation.
167
- The ``explain()`` method provides both the winning plan (the plan MongoDB
168
- executed) and rejected plans.
178
+ The ``explain()`` method provides both the winning plan, which is the plan MongoDB
179
+ executed, and any rejected plans.
180
+
181
+ .. tip::
182
+
183
+ To learn more about query plans and execution statistics, see
184
+ :manual:`Explain Results </reference/explain-results/>` in the Server manual.
169
185
170
186
.. include:: /includes/fundamentals/explain-verbosity.rst
171
187
172
- In the following example, we print the JSON representation of the
173
- winning plans for aggregation stages that produce execution plans:
188
+ The following example prints the JSON representation of the
189
+ winning plans for any aggregation stages that produce execution plans:
174
190
175
191
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
176
192
:language: java
177
193
:dedent:
178
- :start-after: begin aggregation three
179
- :end-before: end aggregation three
194
+ :start-after: begin aggregation explain
195
+ :end-before: end aggregation explain
180
196
181
- The preceding code snippet should produce the following output:
197
+ The example produces the following output as the ``$group`` stage
198
+ is the only stage that produces an execution plan:
182
199
183
200
.. code-block:: none
184
201
:copyable: false
185
202
186
- { "stage": "PROJECTION_SIMPLE",
187
- "transformBy": {"stars": 1, "_id": 0},
203
+ {
204
+ "stage": "GROUP",
205
+ "planNodeId": 2,
188
206
"inputStage": {
189
- "stage": "COLLSCAN",
190
- "filter": {
191
- "categories": {"$eq":"bakery"}},
192
- "direction": "forward"}}
207
+ "stage": "COLLSCAN",
208
+ "planNodeId": 1,
209
+ "filter": {
210
+ "categories": {
211
+ "$eq": "Bakery"
212
+ }
213
+ },
214
+ "direction": "forward"
215
+ }
216
+ }
193
217
194
218
For more information about the topics mentioned in this section, see the
195
219
following resources:
@@ -203,7 +227,7 @@ following resources:
203
227
Aggregation Expression Example
204
228
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205
229
206
- The Java driver provides builders for accumulator expressions for use with
230
+ The {+ driver-short+} provides builders for accumulator expressions for use with
207
231
``$group``. You must declare all other expressions in JSON format or
208
232
compatible document format.
209
233
@@ -213,7 +237,7 @@ compatible document format.
213
237
expression.
214
238
215
239
The ``$`` in front of "categories" tells MongoDB that this is a :manual:`field path </meta/aggregation-quick-reference/#expressions>`,
216
- using the " categories" field from the input document.
240
+ using the `` categories`` field from the input document.
217
241
218
242
.. code-block:: java
219
243
:copyable: false
@@ -225,6 +249,10 @@ compatible document format.
225
249
226
250
Document.parse("{ $arrayElemAt: ['$categories', 0] }")
227
251
252
+ Alternatively, you can construct expressions by using the Aggregation
253
+ Expression Operations API. To learn more, see
254
+ :ref:`java-aggregation-expression-operations`.
255
+
228
256
In the following example, the aggregation pipeline uses a
229
257
``$project`` stage and various ``Projections`` to return the ``name``
230
258
field and the calculated field ``firstCategory`` whose value is the
@@ -233,8 +261,8 @@ first element in the ``categories`` field.
233
261
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
234
262
:language: java
235
263
:dedent:
236
- :start-after: begin aggregation two
237
- :end-before: end aggregation two
264
+ :start-after: begin aggregation expression
265
+ :end-before: end aggregation expression
238
266
239
267
The preceding aggregation should produce the following results:
240
268
0 commit comments