@@ -7,23 +7,142 @@ Updates with Aggregation Pipeline
77.. meta::
88 :description: The following page provides examples of updates with aggregation pipelines.
99
10+ .. contents:: On this page
11+ :local:
12+ :backlinks: none
13+ :depth: 2
14+ :class: singlecol
15+
1016Starting in MongoDB 4.2, you can use the aggregation pipeline for
11- update operations. With the update operations, the aggregation
12- pipeline can consist of the following stages:
17+ update operations. You can build and execute aggregation pipelines to
18+ perform updates in `{+atlas+} <https://www.mongodb.com/docs/atlas>`__,
19+ `MongoDB Compass <https://www.mongodb.com/docs/compass/>`__,
20+ `MongoDB Shell <https://www.mongodb.com/docs/mongodb-shell/>`__, or
21+ `Drivers <https://www.mongodb.com/docs/drivers/>`__.
22+
23+ With the update operations, the aggregation pipeline can consist of the
24+ following stages:
1325
14- - :pipeline:`$addFields`
15- - :pipeline:`$set`
16- - :pipeline:`$project`
17- - :pipeline:`$unset`
18- - :pipeline:`$replaceRoot`
19- - :pipeline:`$replaceWith`
26+ .. include:: /includes/aggregation/update-aggregation-stages.rst
2027
2128Using the aggregation pipeline allows for a more expressive update
2229statement, such as expressing conditional updates based on current
2330field values or updating one field using the value of another field(s).
2431
25- Example 1
26- ----------
32+ Create an Update Aggregation Pipeline in Atlas
33+ ----------------------------------------------
34+
35+ You can use the {+atlas+} UI to build an aggregation pipeline to perform
36+ updates. To create and execute aggregation pipelines in the
37+ {+atlas+} UI, you must have the
38+ :authrole:`Project Data Access Read Only` role or higher.
39+
40+ .. procedure::
41+ :style: normal
42+
43+ .. step:: Access the Aggregation Pipeline Builder.
44+
45+ .. procedure::
46+ :style: connected
47+
48+ .. step:: Select the database for the collection.
49+
50+ The main panel and :guilabel:`Namespaces` on the left side list the
51+ collections in the database.
52+
53+ .. step:: Select the collection.
54+
55+ Select the collection on the left-hand side or in the main panel.
56+ The main panel displays the :guilabel:`Find`, :guilabel:`Indexes`,
57+ and :guilabel:`Aggregation` views.
58+
59+ .. step:: Select the Aggregation view.
60+
61+ When you first open the :guilabel:`Aggregation` view, Atlas
62+ displays an empty aggregation pipeline.
63+
64+ .. step:: Create an aggregation pipeline to perform updates.
65+
66+ .. procedure::
67+ :style: connected
68+
69+ .. step:: Select an aggregation stage.
70+
71+ Select an aggregation stage from the :guilabel:`Select`
72+ drop-down menu in the bottom-left panel.
73+
74+ The toggle to the right of the drop-down menu dictates whether
75+ the stage is enabled.
76+
77+ To perform updates with an aggregation, use one of
78+ these stages:
79+
80+ .. include:: /includes/aggregation/update-aggregation-stages.rst
81+
82+ .. step:: Fill in your aggregation stage.
83+
84+ Fill in your stage with the appropriate values.
85+ If :ref:`Comment Mode <pipeline-settings>` is enabled, the pipeline
86+ builder provides syntactic guidelines for your selected stage.
87+
88+ As you modify your stage, Atlas updates the preview documents on
89+ the right based on the results of the current stage.
90+
91+ For examples of what you might include in your aggregation stage,
92+ see the :ref:`examples <agg-updates-examples>` on this page.
93+
94+ Add stages as needed. For more information on creating aggregation
95+ pipelines in Atlas, refer to :ref:`agg-pipeline-create`.
96+
97+ .. step:: Export the aggregation pipeline.
98+
99+ .. procedure::
100+ :style: connected
101+
102+ .. step:: Click Export to Language.
103+
104+ You can find this button at the top of the pipeline builder.
105+
106+ .. step:: Select your desired export language.
107+
108+ In the :guilabel:`Export Pipeline To` menu, select your desired
109+ language.
110+
111+ The :guilabel:`My Pipeline` pane on the left displays your
112+ pipeline in |mdb-shell| syntax. You can copy this directly to execute
113+ your pipeline in the |mdb-shell|.
114+
115+ The pane on the right displays your pipeline in the selected
116+ language. Select your preferred language.
117+
118+ .. step:: Select options, if desired.
119+
120+ *(Optional)*: Check the :guilabel:`Include Import Statements` option
121+ to include the required import statements for the language selected.
122+
123+ *(Optional)*: Check the :guilabel:`Include Driver Syntax` option
124+ to include Driver-specific code to:
125+
126+ - Initialize the client
127+ - Specify the database and collection
128+ - Perform the aggregation operation
129+
130+ .. step:: Copy the pipeline.
131+
132+ Click the :guilabel:`Copy` button at the top-right of the pipeline
133+ to copy the pipeline for the selected language to your clipboard.
134+ Paste the copied pipeline into your application.
135+
136+ .. _agg-updates-examples:
137+
138+ Examples
139+ --------
140+
141+ The following examples demonstrate how to use the aggregation pipeline
142+ stages ``$set``, ``$replaceRoot``, and ``$addFields`` to perform updates.
143+
144+ updateOne with $set
145+ ~~~~~~~~~~~~~~~~~~~
27146
28147Create an example ``students`` collection (if the collection does
29148not currently exist, insert operations will create the collection):
@@ -62,8 +181,8 @@ To verify the update, you can query the collection:
62181
63182 db.students.find().pretty()
64183
65- Example 2
66- ----------
184+ updateMany with $replaceRoot and $set
185+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67186
68187Create an example ``students2`` collection (if the collection does not
69188currently exist, insert operations will create the collection):
@@ -119,8 +238,8 @@ To verify the update, you can query the collection:
119238
120239 db.students2.find()
121240
122- Example 3
123- ---------
241+ updateMany with $set
242+ ~~~~~~~~~~~~~~~~~~~~
124243
125244Create an example ``students3`` collection (if the collection does not
126245currently exist, insert operations will create the collection):
@@ -180,8 +299,8 @@ To verify the update, you can query the collection:
180299
181300 db.students3.find()
182301
183- Example 4
184- ---------
302+ updateOne with $set
303+ ~~~~~~~~~~~~~~~~~~~
185304
186305Create an example ``students4`` collection (if the collection does
187306not currently exist, insert operations will create the collection):
@@ -217,8 +336,8 @@ To verify the update, query the collection:
217336
218337 db.students4.find()
219338
220- Example 5
221- ---------
339+ updateMany with $addFields
340+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
222341
223342Create an example ``temperatures`` collection that contains
224343temperatures in Celsius (if the collection does not currently exist,
@@ -270,7 +389,7 @@ To verify the update, you can query the collection:
270389 db.temperatures.find()
271390
272391Additional Examples
273- -------------------
392+ ~~~~~~~~~~~~~~~~~~~
274393
275394See also the various update method pages for additional examples:
276395
0 commit comments