1
- .. _aggregation-top-level-server :
1
+ .. _aggregation-pipeline :
2
2
3
3
====================
4
4
Aggregation Pipeline
@@ -12,66 +12,92 @@ 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
53
MongoDB provides the :method:`db.collection.aggregate()` method in the
53
54
:binary:`~bin.mongo` shell and the :dbcommand:`aggregate` command to
54
- run the aggregation pipeline.
55
+ run the aggregation pipeline.
55
56
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:
59
58
60
- Starting in MongoDB 4.2, you can use the aggregation pipeline for
61
- updates in:
59
+ Aggregation Pipeline Stages
60
+ ---------------------------
62
61
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.
64
66
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.
66
70
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
68
84
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
70
94
95
+ .. _aggregation-pipeline-expressions:
96
+
71
97
Pipeline Expressions
72
98
--------------------
73
99
74
- Some pipeline stages take a pipeline expression as the operand.
100
+ Some pipeline stages accept a pipeline expression as the operand.
75
101
Pipeline expressions specify the transformation to apply to the input
76
102
documents. Expressions have a :doc:`document </core/document>`
77
103
structure and can contain other :ref:`expression
0 commit comments