1+ .. _java-aggregation:
2+
13===========
24Aggregation
35===========
46
7+ .. facet::
8+ :name: genre
9+ :values: reference
10+
11+ .. meta::
12+ :keywords: code example, transform, computed
13+
514.. contents:: On this page
615 :local:
716 :backlinks: none
@@ -11,39 +20,41 @@ Aggregation
1120Overview
1221--------
1322
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**.
1725
1826Aggregation 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.
2331
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.
2737
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.
3041
31- Aggregation and Find Operations Compared
32- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+ Compare Aggregation and Find Operations
43+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3344
34- Using `` find`` operations, you can :
45+ You can use find operations to perform the following actions :
3546
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
3950
40- Using `` aggregation`` operations, you can :
51+ You can use aggregation operations to perform the following actions :
4152
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
4758
4859Aggregation operations have some :manual:`limitations </core/aggregation-pipeline-limits/>` you must keep in mind:
4960
@@ -70,8 +81,8 @@ Useful References
7081Runnable Examples
7182-----------------
7283
73- Base Setup
74- ~~~~~~~~~~
84+ Import Classes
85+ ~~~~~~~~~~~~~~
7586
7687Create a new Java file called ``AggTour.java`` and include the following import statements:
7788
@@ -81,7 +92,6 @@ Create a new Java file called ``AggTour.java`` and include the following import
8192 :start-after: begin imports
8293 :end-before: end imports
8394
84-
8595Connect to a MongoDB Deployment
8696+++++++++++++++++++++++++++++++
8797
@@ -91,22 +101,23 @@ Connect to a MongoDB Deployment
91101
92102 public static void main(String[] args) {
93103 // Replace the uri string with your MongoDB deployment's connection string
94- String uri = "<connection string uri >";
104+ String uri = "<connection string>";
95105
96106 MongoClient mongoClient = MongoClients.create(uri);
97107 MongoDatabase database = mongoClient.getDatabase("aggregation");
98108 MongoCollection<Document> collection = database.getCollection("restaurants");
99109
100- // aggregation here
110+ // Paste the aggregation code here
101111 }
102112 }
103113
104- .. seealso ::
114+ .. tip ::
105115
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>`.
107118
108- Insert the Data
109- +++++++++++++++
119+ Insert Sample Data
120+ ++++++++++++++++++
110121
111122.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
112123 :language: java
@@ -133,15 +144,15 @@ In the following example, the aggregation pipeline:
133144- Uses a :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching documents by the ``stars``
134145 field, accumulating a count of documents for each distinct value of ``stars``.
135146
136- .. seealso ::
147+ .. note ::
137148
138149 You can build the expressions used in this example using the :ref:`aggregation builders <aggregates-builders>`.
139150
140151.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
141152 :language: java
142153 :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
145156
146157The preceding aggregation should produce the following results:
147158
@@ -164,32 +175,45 @@ To view information about how MongoDB executes your operation, use the
164175``explain()`` method of the ``AggregateIterable`` class. The ``explain()``
165176method returns **execution plans** and performance statistics. An execution
166177plan 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.
169185
170186.. include:: /includes/fundamentals/explain-verbosity.rst
171187
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:
174190
175191.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
176192 :language: java
177193 :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
180196
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:
182199
183200.. code-block:: none
184201 :copyable: false
185202
186- { "stage": "PROJECTION_SIMPLE",
187- "transformBy": {"stars": 1, "_id": 0},
203+ {
204+ "stage": "GROUP",
205+ "planNodeId": 2,
188206 "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+ }
193217
194218For more information about the topics mentioned in this section, see the
195219following resources:
@@ -203,7 +227,7 @@ following resources:
203227Aggregation Expression Example
204228~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205229
206- The Java driver provides builders for accumulator expressions for use with
230+ The {+ driver-short+} provides builders for accumulator expressions for use with
207231``$group``. You must declare all other expressions in JSON format or
208232compatible document format.
209233
@@ -213,7 +237,7 @@ compatible document format.
213237 expression.
214238
215239 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.
217241
218242 .. code-block:: java
219243 :copyable: false
@@ -225,6 +249,10 @@ compatible document format.
225249
226250 Document.parse("{ $arrayElemAt: ['$categories', 0] }")
227251
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+
228256In the following example, the aggregation pipeline uses a
229257``$project`` stage and various ``Projections`` to return the ``name``
230258field and the calculated field ``firstCategory`` whose value is the
@@ -233,8 +261,8 @@ first element in the ``categories`` field.
233261.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
234262 :language: java
235263 :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
238266
239267The preceding aggregation should produce the following results:
240268
0 commit comments