1
- .. _aggregation-top-level-server :
1
+ .. _aggregation-pipeline :
2
2
3
3
====================
4
4
Aggregation Pipeline
@@ -12,65 +12,88 @@ Aggregation Pipeline
12
12
:depth: 1
13
13
:class: singlecol
14
14
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:
19
23
20
24
.. code-block:: javascript
21
25
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
+ ] )
26
34
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
30
36
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:
34
38
35
- .. _aggregation-pipeline:
39
+ .. code-block:: javascript
40
+ :copyable: false
36
41
37
- Pipeline
38
- --------
42
+ [
43
+ { _id: 'Steel beam', sumQuantity: 50 },
44
+ { _id: 'Iron rod', sumQuantity: 60 }
45
+ ]
39
46
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::
45
48
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`
51
52
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:
54
54
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
+ ---------------------------
58
57
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.
61
62
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.
63
66
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:
65
78
66
- :doc:`/tutorial/update-documents-with-aggregation-pipeline`
79
+ - :method:`db.collection.aggregate()` or
67
80
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
69
90
91
+ .. _aggregation-pipeline-expressions:
92
+
70
93
Pipeline Expressions
71
94
--------------------
72
95
73
- Some pipeline stages take a pipeline expression as the operand.
96
+ Some pipeline stages accept a pipeline expression as the operand.
74
97
Pipeline expressions specify the transformation to apply to the input
75
98
documents. Expressions have a :doc:`document </core/document>`
76
99
structure and can contain other :ref:`expression
0 commit comments