1- .. _aggregation-top-level-server :
1+ .. _aggregation-pipeline :
22
33====================
44Aggregation Pipeline
@@ -12,65 +12,88 @@ 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
52- MongoDB provides the :method:`db.collection.aggregate()` shell method
53- and the :dbcommand:`aggregate` command to run the aggregation pipeline.
53+ .. _aggregation-pipeline-stages:
5454
55- For example usage of the aggregation pipeline, consider
56- :doc:`/tutorial/aggregation-with-user-preference-data` and
57- :doc:`/tutorial/aggregation-zip-code-data-set`.
55+ Aggregation Pipeline Stages
56+ ---------------------------
5857
59- Starting in MongoDB 4.2, you can use the aggregation pipeline for
60- updates in:
58+ An aggregation pipeline consists of one or more :ref:`stages
59+ <aggregation-pipeline-operator-reference>` that process documents:
60+
61+ - Each stage transforms the documents as they pass through the pipeline.
6162
62- .. include:: /includes/table-update-with-aggregation-availability.rst
63+ - A stage does not have to output one document for every input
64+ document. For example, some stages may produce new documents or
65+ filter out documents.
6366
64- .. seealso::
67+ - The same stage can appear multiple times in the pipeline with these
68+ stage exceptions: :pipeline:`$out`, :pipeline:`$merge`, and
69+ :pipeline:`$geoNear`.
70+
71+ - For all available stages, see
72+ :ref:`aggregation-pipeline-operator-reference`.
73+
74+ Run an Aggregation Pipeline
75+ ---------------------------
76+
77+ To run an aggregation pipeline, use:
6578
66- :doc:`/tutorial/update-documents-with-aggregation-pipeline`
79+ - :method:`db.collection.aggregate()` or
6780
68- .. _aggregation-pipeline-expressions:
81+ - :dbcommand:`aggregate`
82+
83+ Update Documents Using an Aggregation Pipeline
84+ ----------------------------------------------
85+
86+ Starting in MongoDB 4.2, you can use the aggregation pipeline to update
87+ documents using these methods:
88+
89+ .. include:: /includes/table-update-with-aggregation-availability.rst
6990
91+ .. _aggregation-pipeline-expressions:
92+
7093Pipeline Expressions
7194--------------------
7295
73- Some pipeline stages take a pipeline expression as the operand.
96+ Some pipeline stages accept a pipeline expression as the operand.
7497Pipeline expressions specify the transformation to apply to the input
7598documents. Expressions have a :doc:`document </core/document>`
7699structure and can contain other :ref:`expression
0 commit comments