1- .. _aggregation-top-level-server :
1+ .. _aggregation-pipeline :
22
33====================
44Aggregation Pipeline
@@ -12,66 +12,92 @@ Aggregation Pipeline
1212 :depth: 1
1313 :class: singlecol
1414
15- The aggregation pipeline is a framework for data aggregation modeled
16- on the concept of data processing pipelines. Documents enter a
17- multi-stage pipeline that transforms the documents into aggregated
18- results. For example:
15+ .. include:: /includes/aggregation-pipeline-introduction.rst
16+
17+ .. _aggregation-pipeline-example:
18+
19+ Complete Aggregation Pipeline Example
20+ -------------------------------------
21+
22+ Create the following collection that contains orders for products:
1923
2024.. code-block:: javascript
2125
22- db.orders.aggregate([
23- { $match: { status: "A" } },
24- { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
25- ])
26+ db.orders.insertMany( [
27+ { _id: 0, productName: "Steel beam", status: "new", quantity: 10 },
28+ { _id: 1, productName: "Steel beam", status: "urgent", quantity: 20 },
29+ { _id: 2, productName: "Steel beam", status: "urgent", quantity: 30 },
30+ { _id: 3, productName: "Iron rod", status: "new", quantity: 15 },
31+ { _id: 4, productName: "Iron rod", status: "urgent", quantity: 50 },
32+ { _id: 5, productName: "Iron rod", status: "urgent", quantity: 10 }
33+ ] )
2634
27- **First Stage**: The :pipeline:`$match` stage filters the documents by
28- the ``status`` field and passes to the next stage those documents that
29- have ``status`` equal to ``"A"``.
35+ .. include:: /includes/aggregation-pipeline-example.rst
3036
31- **Second Stage**: The :pipeline:`$group` stage groups the documents by
32- the ``cust_id`` field to calculate the sum of the amount for each
33- unique ``cust_id``.
37+ Example output:
3438
35- .. _aggregation-pipeline:
39+ .. code-block:: javascript
40+ :copyable: false
3641
37- Pipeline
38- --------
42+ [
43+ { _id: 'Steel beam', sumQuantity: 50 },
44+ { _id: 'Iron rod', sumQuantity: 60 }
45+ ]
3946
40- The MongoDB aggregation pipeline consists of :ref:`stages
41- <aggregation-pipeline-operator-reference>`. Each stage transforms the
42- documents as they pass through the pipeline. Pipeline stages do not need
43- to produce one output document for every input document. For example,
44- some stages may generate new documents or filter out documents.
47+ .. seealso::
4548
46- Pipeline stages can appear multiple times in the pipeline with the
47- exception of :pipeline:`$out`, :pipeline:`$merge`, and
48- :pipeline:`$geoNear` stages. For a list
49- of all available stages, see
50- :ref:`aggregation-pipeline-operator-reference`.
49+ - :doc:`/tutorial/aggregation-with-user-preference-data`
50+ - :doc:`/tutorial/aggregation-zip-code-data-set`
51+ - :doc:`/tutorial/update-documents-with-aggregation-pipeline`
5152
5253MongoDB provides the :method:`db.collection.aggregate()` method in the
5354:binary:`~bin.mongo` shell and the :dbcommand:`aggregate` command to
54- run the aggregation pipeline.
55+ run the aggregation pipeline.
5556
56- For example usage of the aggregation pipeline, consider
57- :doc:`/tutorial/aggregation-with-user-preference-data` and
58- :doc:`/tutorial/aggregation-zip-code-data-set`.
57+ .. _aggregation-pipeline-stages:
5958
60- Starting in MongoDB 4.2, you can use the aggregation pipeline for
61- updates in:
59+ Aggregation Pipeline Stages
60+ ---------------------------
6261
63- .. include:: /includes/table-update-with-aggregation-availability.rst
62+ An aggregation pipeline consists of one or more :ref:`stages
63+ <aggregation-pipeline-operator-reference>` that process documents:
64+
65+ - Each stage transforms the documents as they pass through the pipeline.
6466
65- .. seealso::
67+ - A stage does not have to output one document for every input
68+ document. For example, some stages may produce new documents or
69+ filter out documents.
6670
67- :doc:`/tutorial/update-documents-with-aggregation-pipeline`
71+ - The same stage can appear multiple times in the pipeline with these
72+ stage exceptions: :pipeline:`$out`, :pipeline:`$merge`, and
73+ :pipeline:`$geoNear`.
74+
75+ - For all available stages, see
76+ :ref:`aggregation-pipeline-operator-reference`.
77+
78+ Run an Aggregation Pipeline
79+ ---------------------------
80+
81+ To run an aggregation pipeline, use:
82+
83+ - :method:`db.collection.aggregate()` or
6884
69- .. _aggregation-pipeline-expressions:
85+ - :dbcommand:`aggregate`
86+
87+ Update Documents Using an Aggregation Pipeline
88+ ----------------------------------------------
89+
90+ Starting in MongoDB 4.2, you can use the aggregation pipeline to update
91+ documents using these methods:
92+
93+ .. include:: /includes/table-update-with-aggregation-availability.rst
7094
95+ .. _aggregation-pipeline-expressions:
96+
7197Pipeline Expressions
7298--------------------
7399
74- Some pipeline stages take a pipeline expression as the operand.
100+ Some pipeline stages accept a pipeline expression as the operand.
75101Pipeline expressions specify the transformation to apply to the input
76102documents. Expressions have a :doc:`document </core/document>`
77103structure and can contain other :ref:`expression
0 commit comments