-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathaggregation.txt
146 lines (104 loc) · 4.7 KB
/
aggregation.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
.. _node-aggregation:
.. _nodejs-aggregation:
===========
Aggregation
===========
.. meta::
:description: Learn to use aggregation operations in the MongoDB Node.js driver to create pipelines for data transformation and summarization.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. _nodejs-aggregation-overview:
Overview
--------
In this guide, you can learn how to use **aggregation operations** in
the MongoDB Node.js driver.
Aggregation operations are expressions you can use to produce reduced
and summarized results in MongoDB. MongoDB's aggregation framework
allows you to create a pipeline that consists of one or more stages,
each of which performs a specific operation on your data.
Analogy
~~~~~~~
You can think of the aggregation pipeline as similar to an automobile factory.
Automobile manufacturing requires the use of assembly stations organized
into assembly lines. Each station has specialized tools, such as
drills and welders. The factory transforms and
assembles the initial parts and materials into finished products.
The **aggregation pipeline** is the assembly line, **aggregation
stages** are the assembly stations, and **expression operators** are the
specialized tools.
Comparing Aggregation and Query Operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using query operations, such as the ``find()`` method, you can perform the following actions:
- Select *which documents* to return
- Select *which fields* to return
- Sort the results
Using aggregation operations, you can perform the following actions:
- Perform all query operations
- Rename fields
- Calculate fields
- Summarize data
- Group values
Aggregation operations have some :manual:`limitations </core/aggregation-pipeline-limits/>`:
- Returned documents must not violate the :manual:`BSON-document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>`
of 16 megabytes.
- Pipeline stages have a memory limit of 100 megabytes by default. You can exceed this
limit by setting the ``allowDiskUse`` property of ``AggregateOptions`` to ``true``. See
the `AggregateOptions API documentation <{+api+}/interfaces/AggregateOptions.html>`__
for more details.
.. important:: $graphLookup exception
The :manual:`$graphLookup
</reference/operator/aggregation/graphLookup/>` stage has a strict
memory limit of 100 megabytes and will ignore ``allowDiskUse``.
References
~~~~~~~~~~
To view a full list of expression operators, see :manual:`Aggregation
Operators </reference/operator/aggregation/>` in the Server manual.
To learn about assembling an aggregation pipeline and view examples, see
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` in the
Server manual.
To learn more about creating pipeline stages, see :manual:`Aggregation
Stages </reference/operator/aggregation-pipeline/>` in the Server manual.
Runnable Examples
-----------------
The example uses sample data about restaurants. The following code
inserts data into the ``restaurants`` collection of the ``aggregation``
database:
.. literalinclude:: /code-snippets/aggregation/agg.js
:start-after: begin data insertion
:end-before: end data insertion
:language: javascript
:dedent:
.. tip::
For more information on connecting to your MongoDB deployment, see the :doc:`Connection Guide </fundamentals/connection>`.
Aggregation Example
~~~~~~~~~~~~~~~~~~~
To perform an aggregation, pass a list of aggregation stages to the
``collection.aggregate()`` method.
In the example, the aggregation pipeline uses the following aggregation stages:
- A :manual:`$match </reference/operator/aggregation/match/>` stage to filter for documents whose
``categories`` array field contains the element ``Bakery``.
- A :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching documents by the ``stars``
field, accumulating a count of documents for each distinct value of ``stars``.
.. literalinclude:: /code-snippets/aggregation/agg.js
:start-after: begin aggregation
:end-before: end aggregation
:language: javascript
:dedent:
This example produces the following output:
.. code-block:: json
:copyable: false
{ _id: 4, count: 2 }
{ _id: 3, count: 1 }
{ _id: 5, count: 1 }
For more information, see the `aggregate() API documentation <{+api+}/classes/Collection.html#aggregate>`__.
Additional Examples
~~~~~~~~~~~~~~~~~~~
To view step-by-step explanations of common aggregation tasks, see the
:ref:`node-aggregation-tutorials-landing`.
You can find another aggregation pipeline example in the `Aggregation
Framework with Node.js Tutorial
<https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-analyze-data-using-the-aggregation-framework>`_
blog post on the MongoDB website.