Skip to content

Commit 2210928

Browse files
jason-price-mongodbjason-price-mongodb
and
jason-price-mongodb
authored
DOCS-14890 refactor start of aggregation pipeline page (#6056) (#6057)
Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 872fd8d commit 2210928

File tree

5 files changed

+126
-66
lines changed

5 files changed

+126
-66
lines changed

source/aggregation.txt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,41 @@ Aggregation
1010
:depth: 1
1111
:class: singlecol
1212

13-
Aggregation operations process data records and return computed
14-
results. Aggregation operations group values from multiple documents
15-
together, and can perform a variety of operations on the grouped data
16-
to return a single result. MongoDB provides three ways to perform
17-
aggregation: the :ref:`aggregation pipeline
18-
<aggregation-framework>`, the :ref:`map-reduce function
19-
<aggregation-map-reduce>`, and :ref:`single purpose aggregation methods
20-
<single-purpose-agg-operations>`.
13+
Aggregation operations process multiple documents and return computed
14+
results. You can use aggregation operations to:
15+
16+
- Group values from multiple documents together.
17+
18+
- Perform operations on the grouped data to return a single result.
19+
20+
- Analyze data changes over time.
21+
22+
To perform aggregation operations, you can use:
23+
24+
- :ref:`Aggregation pipelines <aggregation-framework>`
25+
26+
- :ref:`Single purpose aggregation methods
27+
<single-purpose-agg-operations>`
28+
29+
- :ref:`Map-reduce functions <aggregation-map-reduce>`
2130

2231
.. _aggregation-framework:
2332

24-
Aggregation Pipeline
25-
--------------------
33+
Aggregation Pipelines
34+
---------------------
2635

27-
MongoDB's :doc:`aggregation framework
28-
</core/aggregation-pipeline>` is modeled on the concept of data
29-
processing pipelines. Documents enter a multi-stage pipeline that
30-
transforms the documents into an aggregated result. For example:
36+
.. include:: /includes/aggregation-pipeline-introduction.rst
3137

32-
.. code-block:: javascript
38+
Aggregation Pipeline Example
39+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3340

34-
db.orders.aggregate([
35-
{ $match: { status: "A" } },
36-
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
37-
])
41+
.. include:: /includes/aggregation-pipeline-example.rst
3842

39-
**First Stage**: The :pipeline:`$match` stage filters the documents by
40-
the ``status`` field and passes to the next stage those documents that
41-
have ``status`` equal to ``"A"``.
43+
For a runnable example, see :ref:`Complete Aggregation Pipeline
44+
Example <aggregation-pipeline-example>`.
4245

43-
**Second Stage**: The :pipeline:`$group` stage groups the documents by
44-
the ``cust_id`` field to calculate the sum of the amount for each
45-
unique ``cust_id``.
46+
Aggregation Pipeline Stages and Operations
47+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4648

4749
The most basic pipeline stages provide *filters* that operate like
4850
queries and *document transformations* that modify the form

source/core/aggregation-pipeline.txt

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _aggregation-top-level-server:
1+
.. _aggregation-pipeline:
22

33
====================
44
Aggregation 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+
7093
Pipeline 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.
7497
Pipeline expressions specify the transformation to apply to the input
7598
documents. Expressions have a :doc:`document </core/document>`
7699
structure and can contain other :ref:`expression
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The following aggregation pipeline example contains two :ref:`stages
2+
<aggregation-pipeline-operator-reference>` and returns the total
3+
quantity of urgent orders for each product:
4+
5+
.. code-block:: javascript
6+
7+
db.orders.aggregate( [
8+
{ $match: { status: "urgent" } },
9+
{ $group: { _id: "$productName", sumQuantity: { $sum: "$quantity" } } }
10+
] )
11+
12+
The :pipeline:`$match` stage:
13+
14+
- Filters the documents to those with a ``status`` of ``urgent``.
15+
16+
- Outputs the filtered documents to the :pipeline:`$group` stage.
17+
18+
The :pipeline:`$group` stage:
19+
20+
- Groups the input documents by ``productName``.
21+
22+
- Uses :group:`$sum` to calculate the total ``quantity`` for each
23+
``productName``, which is stored in the ``sumQuantity`` field returned
24+
by the aggregation pipeline.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
An aggregation pipeline consists of one or more :ref:`stages
2+
<aggregation-pipeline-operator-reference>` that process documents:
3+
4+
- Each stage performs an operation on the input documents.
5+
For example, a stage can filter documents, group documents, and
6+
calculate values.
7+
8+
- The documents that are output from one stage are input to the next
9+
stage.
10+
11+
- An aggregation pipeline can return results for groups of documents.
12+
For example, return the total, average, maximum, and minimum values.

source/includes/table-update-with-aggregation-availability.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
- | :ref:`db.collection.updateOne() <updateOne-example-agg>`
1515
| :ref:`db.collection.updateMany() <updateMany-example-agg>`
16-
| :ref:`db.collection.update() <update-example-agg>`
1716
1817
| :ref:`Bulk.find.update() <example-bulk-find-update-agg>`
1918
| :ref:`Bulk.find.updateOne() <example-bulk-find-update-one-agg>`

0 commit comments

Comments
 (0)