Skip to content

Commit 3621c3a

Browse files
committed
aggregation
1 parent 1bc5de1 commit 3621c3a

File tree

1 file changed

+6
-210
lines changed

1 file changed

+6
-210
lines changed

source/aggregation.txt

Lines changed: 6 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ The **aggregation pipeline** is the assembly line, **aggregation
4545
stages** are the assembly stations, and **operator expressions** are the
4646
specialized tools.
4747

48+
For more information about aggregation in the {+driver-short+}, see the
49+
following pages:
50+
51+
- :ref:`<java-aggregation-expression-operations>`
52+
- :ref:`<java-aggregation-examples>`
53+
4854
Compare Aggregation and Find Operations
4955
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5056

@@ -83,213 +89,3 @@ Useful References
8389
- :manual:`Aggregation stages </meta/aggregation-quick-reference/#stages>`
8490
- :manual:`Operator expressions </meta/aggregation-quick-reference/#operator-expressions>`
8591
- :ref:`Aggregation Builders <aggregates-builders>`
86-
87-
Runnable Examples
88-
-----------------
89-
90-
Import Classes
91-
~~~~~~~~~~~~~~
92-
93-
Create a new Java file called ``AggTour.java`` and include the following import statements:
94-
95-
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
96-
:language: java
97-
:dedent:
98-
:start-after: begin imports
99-
:end-before: end imports
100-
101-
Connect to a MongoDB Deployment
102-
+++++++++++++++++++++++++++++++
103-
104-
.. code-block:: java
105-
106-
public class AggTour {
107-
108-
public static void main(String[] args) {
109-
// Replace the uri string with your MongoDB deployment's connection string
110-
String uri = "<connection string>";
111-
112-
MongoClient mongoClient = MongoClients.create(uri);
113-
MongoDatabase database = mongoClient.getDatabase("aggregation");
114-
MongoCollection<Document> collection = database.getCollection("restaurants");
115-
116-
// Paste the aggregation code here
117-
}
118-
}
119-
120-
.. tip::
121-
122-
To learn more about connecting to MongoDB, see the :ref:`Connection
123-
Guide <java-connect-to-mongodb>`.
124-
125-
Insert Sample Data
126-
++++++++++++++++++
127-
128-
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
129-
:language: java
130-
:dedent:
131-
:start-after: begin insert
132-
:end-before: end insert
133-
134-
Basic Aggregation Example
135-
~~~~~~~~~~~~~~~~~~~~~~~~~
136-
137-
To perform an aggregation, pass a list of aggregation stages to the
138-
``MongoCollection.aggregate()`` method.
139-
140-
The Java driver provides the
141-
`Aggregates <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html>`__
142-
helper class that contains builders for aggregation stages.
143-
144-
In the following example, the aggregation pipeline:
145-
146-
- Uses a :manual:`$match </reference/operator/aggregation/match/>` stage to filter for documents whose
147-
``categories`` array field contains the element ``Bakery``. The example uses
148-
``Aggregates.match`` to build the ``$match`` stage.
149-
150-
- Uses a :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching documents by the ``stars``
151-
field, accumulating a count of documents for each distinct value of ``stars``.
152-
153-
.. note::
154-
155-
You can build the expressions used in this example using the :ref:`aggregation builders <aggregates-builders>`.
156-
157-
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
158-
:language: java
159-
:dedent:
160-
:start-after: begin aggregation basic
161-
:end-before: end aggregation basic
162-
163-
The preceding aggregation produces the following results:
164-
165-
.. code-block:: none
166-
:copyable: false
167-
168-
{"_id": 4, "count": 2}
169-
{"_id": 5, "count": 1}
170-
171-
For more information about the methods and classes mentioned in this section,
172-
see the following API Documentation:
173-
174-
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
175-
- `Aggregates.match <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#match(org.bson.conversions.Bson)>`__
176-
177-
Explain Aggregation Example
178-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
179-
180-
To view information about how MongoDB executes your operation, use the
181-
``explain()`` method of the ``AggregateIterable`` class. The ``explain()``
182-
method returns **execution plans** and performance statistics. An execution
183-
plan is a potential way MongoDB can complete an operation.
184-
The ``explain()`` method provides both the winning plan, which is the plan MongoDB
185-
executed, and any rejected plans.
186-
187-
.. tip::
188-
189-
To learn more about query plans and execution statistics, see
190-
:manual:`Explain Results </reference/explain-results/>` in the Server manual.
191-
192-
.. include:: /includes/fundamentals/explain-verbosity.rst
193-
194-
The following example prints the JSON representation of the
195-
winning plans for any aggregation stages that produce execution plans:
196-
197-
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
198-
:language: java
199-
:dedent:
200-
:start-after: begin aggregation explain
201-
:end-before: end aggregation explain
202-
203-
The example produces the following output as the ``$group`` stage
204-
is the only stage that produces an execution plan:
205-
206-
.. code-block:: none
207-
:copyable: false
208-
209-
{
210-
"stage": "GROUP",
211-
"planNodeId": 2,
212-
"inputStage": {
213-
"stage": "COLLSCAN",
214-
"planNodeId": 1,
215-
"filter": {
216-
"categories": {
217-
"$eq": "Bakery"
218-
}
219-
},
220-
"direction": "forward"
221-
}
222-
}
223-
224-
For more information about the topics mentioned in this section, see the
225-
following resources:
226-
227-
- :manual:`Explain Output </reference/explain-results/>` Server Manual Entry
228-
- :manual:`Query Plans </core/query-plans/>` Server Manual Entry
229-
- `ExplainVerbosity <{+api+}/apidocs/mongodb-driver-core/com/mongodb/ExplainVerbosity>`__ API Documentation
230-
- `explain() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#explain()>`__ API Documentation
231-
- `AggregateIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/AggregateIterable.html>`__ API Documentation
232-
233-
Aggregation Expression Example
234-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235-
236-
The {+driver-short+} provides builders for accumulator expressions for use with
237-
``$group``. You must declare all other expressions in JSON format or
238-
compatible document format.
239-
240-
.. tip::
241-
242-
The syntax in either of the following examples will define an :manual:`$arrayElemAt </reference/operator/aggregation/arrayElemAt/>`
243-
expression.
244-
245-
The ``$`` in front of "categories" tells MongoDB that this is a :manual:`field path </meta/aggregation-quick-reference/#expressions>`,
246-
using the ``categories`` field from the input document.
247-
248-
.. code-block:: java
249-
:copyable: false
250-
251-
new Document("$arrayElemAt", Arrays.asList("$categories", 0))
252-
253-
.. code-block:: java
254-
:copyable: false
255-
256-
Document.parse("{ $arrayElemAt: ['$categories', 0] }")
257-
258-
Alternatively, you can construct expressions by using the Aggregation
259-
Expression Operations API. To learn more, see
260-
:ref:`java-aggregation-expression-operations`.
261-
262-
In the following example, the aggregation pipeline uses a
263-
``$project`` stage and various ``Projections`` to return the ``name``
264-
field and the calculated field ``firstCategory`` whose value is the
265-
first element in the ``categories`` field.
266-
267-
.. literalinclude:: /includes/fundamentals/code-snippets/AggTour.java
268-
:language: java
269-
:dedent:
270-
:start-after: begin aggregation expression
271-
:end-before: end aggregation expression
272-
273-
The preceding aggregation produces the following results:
274-
275-
.. code-block:: none
276-
:copyable: false
277-
278-
{"name": "456 Cookies Shop", "firstCategory": "Bakery"}
279-
{"name": "Sun Bakery Trattoria", "firstCategory": "Pizza"}
280-
{"name": "456 Steak Restaurant", "firstCategory": "Steak"}
281-
{"name": "Blue Bagels Grill", "firstCategory": "Bagels"}
282-
{"name": "XYZ Steak Buffet", "firstCategory": "Steak"}
283-
{"name": "Hot Bakery Cafe", "firstCategory": "Bakery"}
284-
{"name": "Green Feast Pizzeria", "firstCategory": "Pizza"}
285-
{"name": "ZZZ Pasta Buffet", "firstCategory": "Pasta"}
286-
{"name": "XYZ Coffee Bar", "firstCategory": "Coffee"}
287-
{"name": "XYZ Bagels Restaurant", "firstCategory": "Bagels"}
288-
289-
For more information about the methods and classes mentioned in this section,
290-
see the following API Documentation:
291-
292-
- `Accumulators <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Accumulators.html>`__
293-
- `$group <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#group(TExpression,java.util.List)>`__
294-
- `$project <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
295-
- `Projections <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Projections.html>`__

0 commit comments

Comments
 (0)