@@ -10,18 +10,18 @@ Overview
10
10
--------
11
11
12
12
The MongoDB aggregation framework provides a means to calculate
13
- aggregate values without having to use :term:`map-reduce`. While
14
- map-reduce is powerful, using map-reduce is more difficult than
13
+ aggregated values without having to use :term:`map-reduce`. While
14
+ map-reduce is powerful, it is often more difficult than
15
15
necessary for many simple aggregation tasks, such as totaling or
16
16
averaging field values.
17
17
18
18
If you're familiar with :term:`SQL`, the aggregation framework
19
19
provides similar functionality to ``GROUP BY`` and related SQL
20
20
operators as well as simple forms of "self joins." Additionally, the
21
- aggregation framework provides projection capabilities to resultshape the
22
- returned data. Using projections and aggregation, you can add computed
23
- fields, create new virtual sub-objects, and extract sub-fields into
24
- the top-level of results.
21
+ aggregation framework provides projection capabilities to reshape the
22
+ returned data. Using the projections in the aggregation framework , you
23
+ can add computed fields, create new virtual sub-objects, and extract
24
+ sub-fields into the top-level of results.
25
25
26
26
.. seealso:: A presentation from MongoSV 2011: `MongoDB's New
27
27
Aggregation Framework <http://www.10gen.com/presentations/mongosv-2011/mongodbs-new-aggregation-framework>`_
@@ -74,28 +74,24 @@ through the pipeline.
74
74
- :agg:pipeline:`$group`
75
75
- :agg:pipeline:`$sort`
76
76
77
- .. - :agg:pipeline:`$out`
78
-
79
77
.. _aggregation-expressions:
80
78
81
79
Expressions
82
80
~~~~~~~~~~~
83
81
84
- Expressions calculate values from documents as they pass through the
85
- pipeline and collect these results with calculated values from the
86
- other documents that have flowed through the pipeline. The
87
- aggregation framework defines expressions in a :term:`JSON` like format using
88
- prefixes.
82
+ :ref:`Expressions <aggregation-expression-operators>` produce output
83
+ document based on calculations performed on input documents. The
84
+ aggregation framework defines expressions using a document format
85
+ using prefixes.
89
86
90
- Often, expressions are stateless and are only evaluated when seen by
91
- the aggregation process. Stateless expressions perform operations such
92
- as adding the values of two fields together or extracting the year
93
- from a date .
87
+ Expressions are stateless and are only evaluated when seen by the
88
+ aggregation process. All aggregation expressions can only operate on
89
+ the current document, in the pipeline, and cannot integrate data from
90
+ other documents .
94
91
95
- The :term:`accumulator` expressions *do* retain state, and the
96
- :agg:pipeline:`$group` operator maintains that state (e.g.
97
- totals, maximums, minimums, and related data.) as documents progress
98
- through the :term:`pipeline`.
92
+ The :term:`accumulator` expressions used in the :agg:pipeline:`$group`
93
+ operator maintain that state (e.g. totals, maximums, minimums, and
94
+ related data) as documents progress through the :term:`pipeline`.
99
95
100
96
.. seealso:: :ref:`Aggregation expressions
101
97
<aggregation-expression-operators>` for additional examples of the
@@ -107,17 +103,15 @@ Use
107
103
Invocation
108
104
~~~~~~~~~~
109
105
110
- Invoke an :term:`aggregation` operation with the :func:`aggregate`
106
+ Invoke an :term:`aggregation` operation with the :func:`aggregate() <db.collection.aggregate()> `
111
107
wrapper in the :program:`mongo` shell or the :dbcommand:`aggregate`
112
- :term:`database command`. Always call :func:`aggregate` on a
113
- collection object, which will determine the documents that contribute
114
- to the beginning of the aggregation :term:`pipeline`. The arguments to
115
- the :func:`aggregate` function specifies a sequence of :ref:`pipeline
108
+ :term:`database command`. Always call :func:`aggregate() <db.collection.aggregate()>` on a
109
+ collection object that determines the input documents to the aggregation :term:`pipeline`.
110
+ The arguments to the :func:`aggregate() <db.collection.aggregate()>` function specifies a sequence of :ref:`pipeline
116
111
operators <aggregation-pipeline-operator-reference>`, where each
117
- :ref:`pipeline operator <aggregation-pipeline-operator-reference>` may
118
- have a number of operands.
112
+ operator may have a number of operands.
119
113
120
- First, consider a :term:`collection` of documents named ``article ``
114
+ First, consider a :term:`collection` of documents named ``articles ``
121
115
using the following format:
122
116
123
117
.. code-block:: javascript
@@ -142,7 +136,7 @@ command:
142
136
143
137
.. code-block:: javascript
144
138
145
- db.article .aggregate(
139
+ db.articles .aggregate(
146
140
{ $project : {
147
141
author : 1,
148
142
tags : 1,
@@ -154,12 +148,12 @@ command:
154
148
} }
155
149
);
156
150
157
- This operation uses the :func:`aggregate` wrapper around the
158
- :term:`database command` :dbcommand:`aggregate`. The aggregation
159
- pipleine begins with the :term:`collection` ``article`` and selects
160
- the ``author`` and ``tags`` fields using the :agg:pipeline:`$project`
161
- aggregation operator, and runs the :agg:expression:`$unwind` and
162
- :agg:expression:`$group` on these fields to pivot the data.
151
+ The aggregation pipeline begins with the :term:`collection`
152
+ ``article`` and selects the ``author`` and ``tags`` fields using the
153
+ :agg:pipeline:`$project` aggregation operator, and runs the
154
+ :agg:expression:`$unwind` operator to produce one output document per
155
+ tag finally uses the :agg:expression:`$group` operator on these fields
156
+ to pivot the data.
163
157
164
158
Result
165
159
~~~~~~
@@ -173,13 +167,7 @@ The aggregation operation in the previous section returns a
173
167
if there was an error
174
168
175
169
As a document, the result is subject to the current :ref:`BSON
176
- Document size <limit-bson-document-size>`.
177
-
178
- .. OMMITED: as $out will not be available in 2.2
179
- ..
180
- .. If you expect the aggregation framework to return a larger result,
181
- .. consider using the use the :agg:pipeline:`$out` pipeline operator to
182
- .. write the output to a collection.
170
+ Document size <limit-bson-document-size>`, which is 16 megabytes.
183
171
184
172
Optimizing Performance
185
173
----------------------
0 commit comments