@@ -14,245 +14,160 @@ Aggregation
14
14
15
15
.. contents:: On this page
16
16
:local:
17
+
17
18
:backlinks: none
18
19
:depth: 2
19
20
:class: singlecol
20
21
21
22
.. toctree::
22
23
24
+ Pipeline Stages </aggregation/pipeline-stages>
23
25
Filtered Subset </aggregation/filtered-subset/>
24
26
Group & Total </aggregation/group-total/>
25
27
Unpack Arrays & Group </aggregation/unpack-arrays/>
26
28
One-to-One Join </aggregation/one-to-one-join/>
27
29
Multi-Field Join </aggregation/multi-field-join/>
28
-
30
+
29
31
.. _nodejs-aggregation-overview:
30
32
31
33
Overview
32
34
--------
33
35
34
- In this guide, you can learn how to use **aggregation operations** in
35
- the MongoDB Node.js driver .
36
+ In this guide, you can learn how to use the MongoDB Node.js Driver to perform
37
+ **aggregation operations** .
36
38
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.
39
+ Aggregation operations process data in your MongoDB collections and return
40
+ computed results. The MongoDB Aggregation framework is modeled on the
41
+ concept of data processing pipelines. Documents enter a pipeline comprised of one or
42
+ more stages, and this pipeline transforms the documents into an aggregated result.
43
+
44
+ To learn more about the aggregation stages supported by the Node.js Driver, see :ref:`Aggregation Stages <>`.
45
+ .. todo-- add link here
41
46
42
47
Analogy
43
48
~~~~~~~
44
49
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>`
50
+ The aggregation pipeline is similar to an automobile factory assembly line. An
51
+ assembly lines has stations with specialized tools that are used to perform
52
+ specific tasks. For example, when building a car, the assembly line begins with
53
+ a frame. As the car frame moves though the assembly line, each station adds a
54
+ new part. The factory transforms and assembles the initial parts, resulting in
55
+ finished cars.
56
+
57
+ The *aggregation pipeline* is the assembly line, the *aggregation stages* are
58
+ the assembly stations, and the *expression operators* are the specialized tools.
59
+
60
+ Compare Aggregation and Find Operations
61
+ ---------------------------------------
62
+
63
+ The following table lists the different tasks you can perform with find
64
+ operations compared to what you can achieve with aggregation
65
+ operations. The aggregation framework provides expanded functionality
66
+ that allows you to transform and manipulate your data.
67
+
68
+ .. list-table::
69
+ :header-rows: 1
70
+ :widths: 50 50
71
+
72
+ * - Find Operations
73
+ - Aggregation Operations
74
+
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
+ - | Select *certain* documents to return
81
+ | Select *which* fields to return
82
+ | Sort the results
83
+ | Limit the results
84
+ | Count the results
85
+ | Group the results
86
+ | Rename fields
87
+ | Compute new fields
88
+ | Summarize data
89
+ | Connect and merge data sets
90
+
91
+ Server Limitations
92
+ ------------------
93
+
94
+ Consider the following :manual:`limitations </core/aggregation-pipeline-limits/>` when performing aggregation operations:
95
+
96
+ - Returned documents must not violate the :manual:`BSON document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>`
75
97
of 16 megabytes.
76
98
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``.
87
-
88
- References
89
- ~~~~~~~~~~
90
-
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>`.
99
+ - Pipeline stages have a memory limit of 100 megabytes by default. If required,
100
+ you can exceed this limit by enabling the `AllowDiskUse
101
+ <https://mongodb.github.io/node-mongodb-native/6.17/interfaces/AggregateOptions.html#allowDiskUse>`__
102
+ property of the ``AggregateOptions`` object that you pass to the
103
+ ``Aggregate()`` method.
117
104
105
+ ..
118
106
Aggregation Example
119
- ~~~~~~~~~~~~~~~~~~~
120
-
107
+ -------------------
108
+ ..
121
109
To perform an aggregation, pass a list of aggregation stages to the
122
110
``collection.aggregate()`` method.
123
-
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
-
111
+ ..
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.
117
+ ..
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:
120
+ ..
121
+ - A :manual:`$match </reference/operator/aggregation/match/>` stage to filter
122
+ for documents whose ``cuisine`` field contains the element ``Bakery``.
123
+ ..
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.
127
+ ..
132
128
.. literalinclude:: /code-snippets/aggregation/agg.js
133
129
:start-after: begin aggregation
134
130
:end-before: end aggregation
135
131
:language: javascript
136
132
:dedent:
137
-
133
+ ..
138
134
This example produces the following output:
139
-
135
+ ..
140
136
.. code-block:: json
141
137
: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
207
-
208
- .. important::
209
-
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.
212
-
213
- If you attempt to run the code without making any changes, you will
214
- encounter a connection error.
215
-
216
- For every tutorial, you must replace the connection string placeholder with
217
- your deployment's connection string.
218
-
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.
223
-
224
- For example, if your connection string is
225
- ``"mongodb+srv://mongodb-example:27017"``, your connection string assignment resembles
226
- the following:
227
-
228
- .. code-block:: javascript
229
- :copyable: false
230
-
231
- const uri = "mongodb+srv://mongodb-example:27017";
232
-
233
- To run the completed file after you modify the template for a
234
- tutorial, run the following command in your shell:
235
-
236
- .. code-block:: bash
237
-
238
- node agg_tutorial.js
239
-
240
- Available Tutorials
241
- ~~~~~~~~~~~~~~~~~~~
242
-
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`
248
-
249
- Additional Examples
250
- ~~~~~~~~~~~~~~~~~~~
251
-
252
- To view step-by-step explanations of common aggregation tasks, see the
253
- :ref:`node-aggregation-tutorials-landing`.
254
-
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.
138
+ ..
139
+ { _id = 'Bronx', count = 71 }
140
+ { _id = 'Brooklyn', count = 173 }
141
+ { _id = 'Staten Island', count = 20 }
142
+ { _id = 'Missing', count = 2 }
143
+ { _id = 'Manhattan', count = 221 }
144
+ { _id = 'Queens', count = 204 }
145
+
146
+ Additional information
147
+ ----------------------
148
+
149
+ To view a full list of expression operators, see
150
+ :manual:`Aggregation Operators </reference/operator/aggregation/>`.
151
+
152
+ ..
153
+ To learn more about assembling an aggregation pipeline and view examples, see
154
+ :manual:`Aggregation Pipeline </core/aggregation-pipeline/>`.
155
+ ..
156
+ To learn more about creating pipeline stages and view examples, see
157
+ :manual:`Aggregation Stages </reference/operator/aggregation-pipeline/>`.
158
+
159
+ To learn about explaining MongoDB aggregation operations, see
160
+ :manual:`Explain Results </reference/explain-results/>` and
161
+ :manual:`Query Plans </core/query-plans/>`.
162
+
163
+ ..
164
+ API Documentation
165
+ ~~~~~~~~~~~~~~~~~
166
+ ..
167
+ For more information about the aggregation operations discussed in this guide, see the
168
+ following API documentation:
169
+ ..
170
+ - `Collection() <https://mongodb.github.io/node-mongodb-native/6.17/classes/Collection.html>`__
171
+ - `aggregate() <https://mongodb.github.io/node-mongodb-native/6.17/classes/Collection.html#aggregate>`__
172
+ - `AggregateOptions <https://mongodb.github.io/node-mongodb-native/6.17/interfaces/AggregateOptions.html>`__
173
+ .. try to find $match and $group api links ..
0 commit comments