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