Skip to content

Commit a2b6748

Browse files
(DOCSP-32356): Revamp Updates with Aggregation page for the top 250 project (#4793) (#4919)
* Draft procedures * Add OTP and missing image * Add more links and update headers * Adjust heading depth * Substitutions apparently don't work with links * Incorporate review feedback * Remove images * Incorporate review feedback * Incorporate review feedback * Arbitrary commit to trigger a staging rebuild Co-authored-by: Dachary <[email protected]>
1 parent c145604 commit a2b6748

File tree

2 files changed

+144
-19
lines changed

2 files changed

+144
-19
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- :pipeline:`$addFields`
2+
- :pipeline:`$set`
3+
- :pipeline:`$project`
4+
- :pipeline:`$unset`
5+
- :pipeline:`$replaceRoot`
6+
- :pipeline:`$replaceWith`

source/tutorial/update-documents-with-aggregation-pipeline.txt

Lines changed: 138 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,142 @@ Updates with Aggregation Pipeline
99
.. meta::
1010
:description: The following page provides examples of updates with aggregation pipelines.
1111

12+
.. contents:: On this page
13+
:local:
14+
:backlinks: none
15+
:depth: 2
16+
:class: singlecol
17+
1218
Starting in MongoDB 4.2, you can use the aggregation pipeline for
13-
update operations. With the update operations, the aggregation
14-
pipeline can consist of the following stages:
19+
update operations. You can build and execute aggregation pipelines to
20+
perform updates in `{+atlas+} <https://www.mongodb.com/docs/atlas>`__,
21+
`MongoDB Compass <https://www.mongodb.com/docs/compass/>`__,
22+
`MongoDB Shell <https://www.mongodb.com/docs/mongodb-shell/>`__, or
23+
`Drivers <https://www.mongodb.com/docs/drivers/>`__.
24+
25+
With the update operations, the aggregation pipeline can consist of the
26+
following stages:
1527

16-
- :pipeline:`$addFields`
17-
- :pipeline:`$set`
18-
- :pipeline:`$project`
19-
- :pipeline:`$unset`
20-
- :pipeline:`$replaceRoot`
21-
- :pipeline:`$replaceWith`
28+
.. include:: /includes/aggregation/update-aggregation-stages.rst
2229

2330
Using the aggregation pipeline allows for a more expressive update
2431
statement, such as expressing conditional updates based on current
2532
field values or updating one field using the value of another field(s).
2633

27-
Example 1
28-
----------
34+
Create an Update Aggregation Pipeline in Atlas
35+
----------------------------------------------
36+
37+
You can use the {+atlas+} UI to build an aggregation pipeline to perform
38+
updates. To create and execute aggregation pipelines in the
39+
{+atlas+} UI, you must have the
40+
:authrole:`Project Data Access Read Only` role or higher.
41+
42+
.. procedure::
43+
:style: normal
44+
45+
.. step:: Access the Aggregation Pipeline Builder.
46+
47+
.. procedure::
48+
:style: connected
49+
50+
.. step:: Select the database for the collection.
51+
52+
The main panel and :guilabel:`Namespaces` on the left side list the
53+
collections in the database.
54+
55+
.. step:: Select the collection.
56+
57+
Select the collection on the left-hand side or in the main panel.
58+
The main panel displays the :guilabel:`Find`, :guilabel:`Indexes`,
59+
and :guilabel:`Aggregation` views.
60+
61+
.. step:: Select the Aggregation view.
62+
63+
When you first open the :guilabel:`Aggregation` view, Atlas
64+
displays an empty aggregation pipeline.
65+
66+
.. step:: Create an aggregation pipeline to perform updates.
67+
68+
.. procedure::
69+
:style: connected
70+
71+
.. step:: Select an aggregation stage.
72+
73+
Select an aggregation stage from the :guilabel:`Select`
74+
drop-down menu in the bottom-left panel.
75+
76+
The toggle to the right of the drop-down menu dictates whether
77+
the stage is enabled.
78+
79+
To perform updates with an aggregation, use one of
80+
these stages:
81+
82+
.. include:: /includes/aggregation/update-aggregation-stages.rst
83+
84+
.. step:: Fill in your aggregation stage.
85+
86+
Fill in your stage with the appropriate values.
87+
If :ref:`Comment Mode <pipeline-settings>` is enabled, the pipeline
88+
builder provides syntactic guidelines for your selected stage.
89+
90+
As you modify your stage, Atlas updates the preview documents on
91+
the right based on the results of the current stage.
92+
93+
For examples of what you might include in your aggregation stage,
94+
see the :ref:`examples <agg-updates-examples>` on this page.
95+
96+
Add stages as needed. For more information on creating aggregation
97+
pipelines in Atlas, refer to :ref:`agg-pipeline-create`.
98+
99+
.. step:: Export the aggregation pipeline.
100+
101+
.. procedure::
102+
:style: connected
103+
104+
.. step:: Click Export to Language.
105+
106+
You can find this button at the top of the pipeline builder.
107+
108+
.. step:: Select your desired export language.
109+
110+
In the :guilabel:`Export Pipeline To` menu, select your desired
111+
language.
112+
113+
The :guilabel:`My Pipeline` pane on the left displays your
114+
pipeline in |mdb-shell| syntax. You can copy this directly to execute
115+
your pipeline in the |mdb-shell|.
116+
117+
The pane on the right displays your pipeline in the selected
118+
language. Select your preferred language.
119+
120+
.. step:: Select options, if desired.
121+
122+
*(Optional)*: Check the :guilabel:`Include Import Statements` option
123+
to include the required import statements for the language selected.
124+
125+
*(Optional)*: Check the :guilabel:`Include Driver Syntax` option
126+
to include Driver-specific code to:
127+
128+
- Initialize the client
129+
- Specify the database and collection
130+
- Perform the aggregation operation
131+
132+
.. step:: Copy the pipeline.
133+
134+
Click the :guilabel:`Copy` button at the top-right of the pipeline
135+
to copy the pipeline for the selected language to your clipboard.
136+
Paste the copied pipeline into your application.
137+
138+
.. _agg-updates-examples:
139+
140+
Examples
141+
--------
142+
143+
The following examples demonstrate how to use the aggregation pipeline
144+
stages ``$set``, ``$replaceRoot``, and ``$addFields`` to perform updates.
145+
146+
updateOne with $set
147+
~~~~~~~~~~~~~~~~~~~
29148

30149
Create an example ``students`` collection (if the collection does
31150
not currently exist, insert operations will create the collection):
@@ -64,8 +183,8 @@ To verify the update, you can query the collection:
64183

65184
db.students.find().pretty()
66185

67-
Example 2
68-
----------
186+
updateMany with $replaceRoot and $set
187+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69188

70189
Create an example ``students2`` collection (if the collection does not
71190
currently exist, insert operations will create the collection):
@@ -121,8 +240,8 @@ To verify the update, you can query the collection:
121240

122241
db.students2.find()
123242

124-
Example 3
125-
---------
243+
updateMany with $set
244+
~~~~~~~~~~~~~~~~~~~~
126245

127246
Create an example ``students3`` collection (if the collection does not
128247
currently exist, insert operations will create the collection):
@@ -182,8 +301,8 @@ To verify the update, you can query the collection:
182301

183302
db.students3.find()
184303

185-
Example 4
186-
---------
304+
updateOne with $set
305+
~~~~~~~~~~~~~~~~~~~
187306

188307
Create an example ``students4`` collection (if the collection does
189308
not currently exist, insert operations will create the collection):
@@ -219,8 +338,8 @@ To verify the update, query the collection:
219338

220339
db.students4.find()
221340

222-
Example 5
223-
---------
341+
updateMany with $addFields
342+
~~~~~~~~~~~~~~~~~~~~~~~~~~
224343

225344
Create an example ``temperatures`` collection that contains
226345
temperatures in Celsius (if the collection does not currently exist,
@@ -272,7 +391,7 @@ To verify the update, you can query the collection:
272391
db.temperatures.find()
273392

274393
Additional Examples
275-
-------------------
394+
~~~~~~~~~~~~~~~~~~~
276395

277396
See also the various update method pages for additional examples:
278397

0 commit comments

Comments
 (0)