forked from mongodb/docs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregation.txt
200 lines (152 loc) · 5.96 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
.. _golang-aggregation:
===========
Aggregation
===========
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Overview
--------
In this guide, you can learn how to use **aggregation operations** in the
{+driver-long+}.
Aggregation operations process data in your MongoDB collections based on
your specifications in the **aggregation pipeline**. An aggregation
pipeline consists of one or more **stages**. Each stage performs an
operation based on its expression operators. After the driver executes
the aggregation pipeline, it returns an aggregated result.
Analogy
~~~~~~~
Aggregation operations operate similarly to a car factory. Car factories
have an assembly line. The assembly lines have assembly stations with
specialized tools to perform a specific task. To build a car, you send
raw parts to the factory. Then, the assembly line transforms and
assembles the parts into a car.
The assembly line resembles the **aggregation pipeline**, the assembly
stations in the assembly line resemble the **aggregation stages**, the
specialized tools represent the **expression operators**, and the
finished product resembles the **aggregated result**.
Compare Operations
------------------
The following table lists the tasks you can perform by using find and aggregation operations.
.. list-table::
:header-rows: 1
:widths: 50 50
* - Find Operations
- Aggregation Operations
* - | Select *what* documents to return
| Select *which* fields to return
| Sort the results
| Limit the results
| Count the results
- | Select *what* documents to return
| Select *which* fields to return
| Sort the results
| Limit the results
| Count the results
| Rename fields
| Calculate fields
| Summarize data
| Group values
Limitations
-----------
Aggregation operations have limitations. When performing aggregation
operations, keep the following in mind:
- Returned documents must not violate the :manual:`BSON document size
limit </reference/limits/#BSON-Document-Size>` of 16 megabytes.
- Pipeline stages have a memory limit of 100 megabytes by default. If
required, you may exceed this limit by using the `allowDiskUse()
<{+api+}/mongo/options#AggregateOptionsBuilder.SetAllowDiskUse>`__
method.
- The :manual:`$graphLookup
</reference/operator/aggregation/graphLookup/>` stage
has a strict memory limit of 100 megabytes and ignores the
``allowDiskUse`` setting.
Examples
--------
The examples in this section use the following ``Tea`` struct as a model for documents
in the ``tea`` collection:
.. literalinclude:: /includes/fundamentals/code-snippets/aggregation.go
:start-after: start-tea-struct
:end-before: end-tea-struct
:language: go
:dedent:
To run the examples in this section, load the sample data into the
``tea`` collection in the ``db`` database by using the following snippet:
.. literalinclude:: /includes/fundamentals/code-snippets/aggregation.go
:start-after: begin insert docs
:end-before: end insert docs
:language: go
:dedent:
Each document contains information about the tea type, the available toppings, and
the price.
Average Rating
~~~~~~~~~~~~~~
The following example calculates and displays the average rating and
number of ratings for each tea category.
The aggregation pipeline uses the ``$group`` stage to group the
documents by the ``category`` field, calculates the average using the
``$avg`` expression operator, and counts the number of documents using
the ``$sum`` expression operator.
.. io-code-block::
:copyable: true
.. input:: /includes/fundamentals/code-snippets/aggregation.go
:start-after: begin average
:end-before: end average
:language: go
:dedent:
.. output::
:language: none
:visible: false
Average price of black tea options: $6.075
Number of black tea options: 4
Average price of green tea options: $5.70
Number of green tea options: 4
Omit Fields in Results
~~~~~~~~~~~~~~~~~~~~~~
The following example matches documents where you can get milk foam as a
topping and lists the two cheapest options.
The aggregation pipeline contains the following stages:
- ``$match`` stage to match documents where the ``toppings`` field contains "milk foam"
- ``$unset`` stage to omit the ``_id`` and ``category`` fields
- ``$sort`` stage to sort the ``price`` and ``toppings`` in ascending order
- ``$limit`` stage to show the first two documents
.. io-code-block::
:copyable: true
.. input:: /includes/fundamentals/code-snippets/aggregation.go
:start-after: begin unset
:end-before: end unset
:language: go
:dedent:
.. output::
:language: none
:visible: false
Tea: Hojicha
Toppings: lemon, ginger, milk foam
Price: $5.55
Tea: Gyokuro
Toppings: berries, milk foam
Price: $5.65
Additional Information
----------------------
To learn more about the terms mentioned, see the following
guides:
- :manual:`Expression Operators </reference/operator/aggregation/>`
- :manual:`Aggregation Pipeline </core/aggregation-pipeline/>`
- :manual:`Aggregation Stages </meta/aggregation-quick-reference/#stages>`
- :manual:`Operator Expressions </meta/aggregation-quick-reference/#operator-expressions>`
- :manual:`Aggregation Pipeline Limits </core/aggregation-pipeline-limits/>`
To view more aggregation examples, see the following guides:
- :ref:`Count <golang-count-aggregation>`
- :ref:`Limit <golang-limit-aggregation>`
- :ref:`Skip <golang-skip-aggregation>`
- :ref:`Text <golang-search-text-aggregation>`
To learn more about the ``Aggregate()`` method and its behavior, see
:ref:`Retrieve Data <golang-retrieve-aggregation>`.
API Documentation
~~~~~~~~~~~~~~~~~
To learn more about any of the methods or types discussed in this
guide, see the following API Documentation:
- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__
- `AggregateOptions <{+api+}/mongo/options#AggregateOptions>`__