Skip to content

Commit 59a2a49

Browse files
committed
DOCSP-50497 Standardizing Node.js Aggregation Page
1 parent 1751141 commit 59a2a49

File tree

2 files changed

+118
-213
lines changed

2 files changed

+118
-213
lines changed

source/aggregation.txt

Lines changed: 116 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -31,228 +31,150 @@ Aggregation
3131
Overview
3232
--------
3333

34-
In this guide, you can learn how to use **aggregation operations** in
35-
the MongoDB Node.js driver.
34+
In this guide, you can learn how to use the MongoDB {+driver+} to perform
35+
**aggregation operations**.
3636

37-
Aggregation operations are expressions you can use to produce reduced
38-
and summarized results in MongoDB. MongoDB's aggregation framework
39-
allows you to create a pipeline that consists of one or more stages,
40-
each of which performs a specific operation on your data.
37+
Aggregation operations process data in your MongoDB collections and return
38+
reduced and summarized results. The MongoDB Aggregation framework is modeled on the
39+
concept of data processing pipelines. Documents enter a pipeline comprised of one or
40+
more stages, and this pipeline transforms the documents into an aggregated result.
4141

4242
Analogy
4343
~~~~~~~
4444

45-
You can think of the aggregation pipeline as similar to an automobile factory.
46-
Automobile manufacturing requires the use of assembly stations organized
47-
into assembly lines. Each station has specialized tools, such as
48-
drills and welders. The factory transforms and
49-
assembles the initial parts and materials into finished products.
50-
51-
The **aggregation pipeline** is the assembly line, **aggregation
52-
stages** are the assembly stations, and **expression operators** are the
53-
specialized tools.
54-
55-
Comparing Aggregation and Query Operations
56-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57-
58-
Using query operations, such as the ``find()`` method, you can perform the following actions:
59-
60-
- Select *which documents* to return
61-
- Select *which fields* to return
62-
- Sort the results
63-
64-
Using aggregation operations, you can perform the following actions:
65-
66-
- Perform all query operations
67-
- Rename fields
68-
- Calculate fields
69-
- Summarize data
70-
- Group values
71-
72-
Aggregation operations have some :manual:`limitations </core/aggregation-pipeline-limits/>`:
73-
74-
- Returned documents must not violate the :manual:`BSON-document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>`
45+
The aggregation pipeline is similar to an automobile factory assembly line. An
46+
assembly lines has stations with specialized tools that are used to perform
47+
specific tasks. For example, when building a car, the assembly line begins with
48+
a frame. As the car frame moves though the assembly line, each station adds a
49+
new part. The factory transforms and assembles the initial parts, resulting in
50+
finished cars.
51+
52+
The *aggregation pipeline* is the assembly line, the *aggregation stages* are
53+
the assembly stations, and the *expression operators* are the specialized tools.
54+
55+
Compare Aggregation and Find Operations
56+
---------------------------------------
57+
58+
The following table lists the different tasks you can perform with find
59+
operations compared to what you can achieve with aggregation
60+
operations. The aggregation framework provides expanded functionality
61+
that allows you to transform and manipulate your data.
62+
63+
.. list-table::
64+
:header-rows: 1
65+
:widths: 50 50
66+
67+
* - Find Operations
68+
- Aggregation Operations
69+
70+
* - | Select *certain* documents to return
71+
| Select *which* fields to return
72+
| Sort the results
73+
| Limit the results
74+
| Count the results
75+
- | Select *certain* documents to return
76+
| Select *which* fields to return
77+
| Sort the results
78+
| Limit the results
79+
| Count the results
80+
| Group the results
81+
| Rename fields
82+
| Compute new fields
83+
| Summarize data
84+
| Connect and merge data sets
85+
86+
Server Limitations
87+
------------------
88+
89+
Consider the following :manual:`limitations </core/aggregation-pipeline-limits/>` when performing aggregation operations:
90+
91+
- Returned documents must not violate the :manual:`BSON document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>`
7592
of 16 megabytes.
7693

77-
- Pipeline stages have a memory limit of 100 megabytes by default. You can exceed this
78-
limit by setting the ``allowDiskUse`` property of ``AggregateOptions`` to ``true``. See
79-
the `AggregateOptions API documentation <{+api+}/interfaces/AggregateOptions.html>`__
80-
for more details.
81-
82-
.. important:: $graphLookup exception
83-
84-
The :manual:`$graphLookup
85-
</reference/operator/aggregation/graphLookup/>` stage has a strict
86-
memory limit of 100 megabytes and will ignore ``allowDiskUse``.
94+
- Pipeline stages have a memory limit of 100 megabytes by default. If required,
95+
you can exceed this limit by enabling the `AllowDiskUse
96+
<{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.AggregateOptions.AllowDiskUse.html#MongoDB_Driver_AggregateOptions_AllowDiskUse>`__
97+
property of the ``AggregateOptions`` object that you pass to the
98+
``Aggregate()`` method.
8799

88-
References
89-
~~~~~~~~~~
100+
- The :manual:`$graphLookup
101+
</reference/operator/aggregation/graphLookup/>` stage has a strict memory
102+
limit of 100 megabytes and will ignore the ``AllowDiskUse`` property.
90103

91-
To view a full list of expression operators, see :manual:`Aggregation
92-
Operators </reference/operator/aggregation/>` in the Server manual.
93-
94-
To learn about assembling an aggregation pipeline and view examples, see
95-
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` in the
96-
Server manual.
97-
98-
To learn more about creating pipeline stages, see :manual:`Aggregation
99-
Stages </reference/operator/aggregation-pipeline/>` in the Server manual.
100-
101-
Runnable Examples
102-
-----------------
103-
104-
The example uses sample data about restaurants. The following code
105-
inserts data into the ``restaurants`` collection of the ``aggregation``
106-
database:
107-
108-
.. literalinclude:: /code-snippets/aggregation/agg.js
109-
:start-after: begin data insertion
110-
:end-before: end data insertion
111-
:language: javascript
112-
:dedent:
113-
114-
.. tip::
115-
116-
For more information on connecting to your MongoDB deployment, see the :doc:`Connection Guide </connect>`.
104+
.. note /code-snippets/aggregation/agg.js deleted - check if file used elsewhere
117105

118106
Aggregation Example
119-
~~~~~~~~~~~~~~~~~~~
107+
-------------------
120108

121109
To perform an aggregation, pass a list of aggregation stages to the
122110
``collection.aggregate()`` method.
123111

124-
In the example, the aggregation pipeline uses the following aggregation stages:
125-
126-
- A :manual:`$match </reference/operator/aggregation/match/>` stage to filter for documents whose
127-
``categories`` array field contains the element ``Bakery``.
128-
129-
- A :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching documents by the ``stars``
130-
field, accumulating a count of documents for each distinct value of ``stars``.
131-
132-
.. literalinclude:: /code-snippets/aggregation/agg.js
133-
:start-after: begin aggregation
134-
:end-before: end aggregation
135-
:language: javascript
136-
:dedent:
137-
138-
This example produces the following output:
139-
140-
.. code-block:: json
141-
:copyable: false
142-
143-
{ _id: 4, count: 2 }
144-
{ _id: 3, count: 1 }
145-
{ _id: 5, count: 1 }
146-
147-
For more information, see the `aggregate() API documentation <{+api+}/classes/Collection.html#aggregate>`__.
148-
149-
.. _node-aggregation-tutorials-landing:
150-
.. _node-aggregation-tutorials:
151-
152-
Aggregation Tutorials
153-
---------------------
154-
155-
Aggregation tutorials provide detailed explanations of common
156-
aggregation tasks in a step-by-step format. The tutorials are adapted
157-
from examples in the `Practical MongoDB Aggregations book
158-
<https://www.practical-mongodb-aggregations.com/>`__ by Paul Done.
159-
160-
Each tutorial includes the following sections:
161-
162-
- **Introduction**, which describes the purpose and common use cases of the
163-
aggregation type. This section also describes the example and desired
164-
outcome that the tutorial demonstrates.
165-
166-
- **Before You Get Started**, which describes the necessary databases,
167-
collections, and sample data that you must have before building the
168-
aggregation pipeline and performing the aggregation.
169-
170-
- **Tutorial**, which describes how to build and run the aggregation
171-
pipeline. This section describes each stage of the completed
172-
aggregation tutorial, and then explains how to run and interpret the
173-
output of the aggregation.
174-
175-
At the end of each aggregation tutorial, you can find a link to a fully
176-
runnable Node.js code file that you can run in your environment.
177-
178-
.. tip::
179-
180-
To learn more about performing aggregations, see the
181-
:ref:`node-aggregation` guide.
182-
183-
.. _node-agg-tutorial-template-app:
184-
185-
Aggregation Template App
186-
~~~~~~~~~~~~~~~~~~~~~~~~
187-
188-
Before you begin following an aggregation tutorial, you must set up a
189-
new Node.js app. You can use this app to connect to a MongoDB
190-
deployment, insert sample data into MongoDB, and run the aggregation
191-
pipeline in each tutorial.
192-
193-
.. tip::
194-
195-
To learn how to install the driver and connect to MongoDB,
196-
see the :ref:`node-get-started-download-and-install` and
197-
:ref:`node-get-started-create-deployment` steps of the
198-
Quick Start guide.
199-
200-
Once you install the driver, create a file called
201-
``agg_tutorial.js``. Paste the following code in this file to create an
202-
app template for the aggregation tutorials:
203-
204-
.. literalinclude:: /includes/aggregation/template-app.js
205-
:language: javascript
206-
:copyable: true
112+
.. note::
113+
114+
This example uses the ``sample_restaurants.restaurants`` collection
115+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
116+
free MongoDB Atlas cluster and load the sample datasets, see the :ref:`Get Started <node-get-started>` guide.
207117

208-
.. important::
118+
The following code example produces a count of the number of bakeries in each borough
119+
of New York City. To do so, the aggregation pipeline uses the following aggregation stages:
209120

210-
In the preceding code, read the code comments to find the sections of
211-
the code that you must modify for the tutorial you are following.
121+
- A :manual:`$match </reference/operator/aggregation/match/>` stage to filter
122+
for documents whose ``cuisine`` field contains the element ``Bakery``.
212123

213-
If you attempt to run the code without making any changes, you will
214-
encounter a connection error.
124+
- A :manual:`$group </reference/operator/aggregation/group/>` stage to group the
125+
matching documents by the ``borough`` field, accumulating a count of documents
126+
for each distinct value in the ``borough`` field.
215127

216-
For every tutorial, you must replace the connection string placeholder with
217-
your deployment's connection string.
128+
.. io-code-block::
218129

219-
.. tip::
220-
221-
To learn how to locate your deployment's connection string, see the
222-
:ref:`node-get-started-connection-string` step of the Quick Start guide.
130+
.. input:: /code-snippets/aggregation/agg.js
131+
:start-after: begin aggregation
132+
:end-before: end aggregation
133+
:language: javascript
134+
:dedent:
223135

224-
For example, if your connection string is
225-
``"mongodb+srv://mongodb-example:27017"``, your connection string assignment resembles
226-
the following:
136+
.. output::
137+
:language: console
138+
:copyable: false
139+
:visible: false
227140

228-
.. code-block:: javascript
229-
:copyable: false
141+
{ _id = Bronx, Count = 71 }
142+
{ _id = Brooklyn, Count = 173 }
143+
{ _id = Staten Island, Count = 20 }
144+
{ _id = Missing, Count = 2 }
145+
{ _id = Manhattan, Count = 221 }
146+
{ _id = Queens, Count = 204 }
230147

231-
const uri = "mongodb+srv://mongodb-example:27017";
148+
Additional information
149+
----------------------
232150

233-
To run the completed file after you modify the template for a
234-
tutorial, run the following command in your shell:
151+
MongoDB Server Manual
152+
~~~~~~~~~~~~~~~~~~~~~
235153

236-
.. code-block:: bash
154+
To view a full list of expression operators, see
155+
:manual:`Aggregation Operators </reference/operator/aggregation/>`.
237156

238-
node agg_tutorial.js
157+
To learn more about assembling an aggregation pipeline and view examples, see
158+
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>`.
239159

240-
Available Tutorials
241-
~~~~~~~~~~~~~~~~~~~
160+
To learn more about creating pipeline stages and view examples, see
161+
:manual:`Aggregation Stages </reference/operator/aggregation-pipeline/>`.
242162

243-
- :ref:`node-aggregation-filtered-subset`
244-
- :ref:`node-aggregation-group-total`
245-
- :ref:`node-aggregation-arrays`
246-
- :ref:`node-aggregation-one-to-one`
247-
- :ref:`node-aggregation-multi-field`
163+
To learn about explaining MongoDB aggregation operations, see
164+
:manual:`Explain Results </reference/explain-results/>` and
165+
:manual:`Query Plans </core/query-plans/>`.
248166

249-
Additional Examples
250-
~~~~~~~~~~~~~~~~~~~
167+
API Documentation
168+
~~~~~~~~~~~~~~~~~
251169

252-
To view step-by-step explanations of common aggregation tasks, see the
253-
:ref:`node-aggregation-tutorials-landing`.
170+
For more information about the aggregation operations discussed in this guide, see the
171+
following API documentation:
254172

255-
You can find another aggregation pipeline example in the `Aggregation
256-
Framework with Node.js Tutorial
257-
<https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-analyze-data-using-the-aggregation-framework>`_
258-
blog post on the MongoDB website.
173+
- `Aggregate() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.Aggregate.html>`__
174+
- `AggregateOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.AggregateOptions.html>`__
175+
- `Group()
176+
<{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.PipelineStageDefinitionBuilder.Group.html>`__
177+
- `Match() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.PipelineStageDefinitionBuilder.Match.html>`__
178+
- `Where() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.Where.html>`__
179+
- `GroupBy() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.GroupBy.html>`__
180+
- `Select() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.Select.html>`__

source/code-snippets/aggregation/agg.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,11 @@ const uri = process.env.MONGDODB_URI;
66
const client = new MongoClient(uri);
77

88
async function run() {
9-
// begin data insertion
10-
const db = client.db("aggregation");
11-
const coll = db.collection("restaurants");
12-
13-
// Create sample documents
14-
const docs = [
15-
{ stars: 3, categories: ["Bakery", "Sandwiches"], name: "Rising Sun Bakery" },
16-
{ stars: 4, categories: ["Bakery", "Cafe", "Bar"], name: "Cafe au Late" },
17-
{ stars: 5, categories: ["Coffee", "Bakery"], name: "Liz's Coffee Bar" },
18-
{ stars: 3, categories: ["Steak", "Seafood"], name: "Oak Steakhouse" },
19-
{ stars: 4, categories: ["Bakery", "Dessert"], name: "Petit Cookie" },
20-
];
21-
22-
// Insert documents into the restaurants collection
23-
const result = await coll.insertMany(docs);
24-
// end data insertion
25-
269
// begin aggregation
2710
// Define an aggregation pipeline with a match stage and a group stage
2811
const pipeline = [
29-
{ $match: { categories: "Bakery" } },
30-
{ $group: { _id: "$stars", count: { $sum: 1 } } }
12+
{ $match: { cuisine: "Bakery" } },
13+
{ $group: { _id: "$borough", count: { $sum: 1 } } }
3114
];
3215

3316
// Execute the aggregation

0 commit comments

Comments
 (0)