@@ -13,23 +13,142 @@ Updates with Aggregation Pipeline
1313.. meta::
1414 :description: The following page provides examples of updates with aggregation pipelines.
1515
16+ .. contents:: On this page
17+ :local:
18+ :backlinks: none
19+ :depth: 2
20+ :class: singlecol
21+
1622Starting in MongoDB 4.2, you can use the aggregation pipeline for
17- update operations. With the update operations, the aggregation
18- pipeline can consist of the following stages:
23+ update operations. You can build and execute aggregation pipelines to
24+ perform updates in `{+atlas+} <https://www.mongodb.com/docs/atlas>`__,
25+ `MongoDB Compass <https://www.mongodb.com/docs/compass/>`__,
26+ `MongoDB Shell <https://www.mongodb.com/docs/mongodb-shell/>`__, or
27+ `Drivers <https://www.mongodb.com/docs/drivers/>`__.
28+
29+ With the update operations, the aggregation pipeline can consist of the
30+ following stages:
1931
20- - :pipeline:`$addFields`
21- - :pipeline:`$set`
22- - :pipeline:`$project`
23- - :pipeline:`$unset`
24- - :pipeline:`$replaceRoot`
25- - :pipeline:`$replaceWith`
32+ .. include:: /includes/aggregation/update-aggregation-stages.rst
2633
2734Using the aggregation pipeline allows for a more expressive update
2835statement, such as expressing conditional updates based on current
2936field values or updating one field using the value of another field(s).
3037
31- Example 1
32- ----------
38+ Create an Update Aggregation Pipeline in Atlas
39+ ----------------------------------------------
40+
41+ You can use the {+atlas+} UI to build an aggregation pipeline to perform
42+ updates. To create and execute aggregation pipelines in the
43+ {+atlas+} UI, you must have the
44+ :authrole:`Project Data Access Read Only` role or higher.
45+
46+ .. procedure::
47+ :style: normal
48+
49+ .. step:: Access the Aggregation Pipeline Builder.
50+
51+ .. procedure::
52+ :style: connected
53+
54+ .. step:: Select the database for the collection.
55+
56+ The main panel and :guilabel:`Namespaces` on the left side list the
57+ collections in the database.
58+
59+ .. step:: Select the collection.
60+
61+ Select the collection on the left-hand side or in the main panel.
62+ The main panel displays the :guilabel:`Find`, :guilabel:`Indexes`,
63+ and :guilabel:`Aggregation` views.
64+
65+ .. step:: Select the Aggregation view.
66+
67+ When you first open the :guilabel:`Aggregation` view, Atlas
68+ displays an empty aggregation pipeline.
69+
70+ .. step:: Create an aggregation pipeline to perform updates.
71+
72+ .. procedure::
73+ :style: connected
74+
75+ .. step:: Select an aggregation stage.
76+
77+ Select an aggregation stage from the :guilabel:`Select`
78+ drop-down menu in the bottom-left panel.
79+
80+ The toggle to the right of the drop-down menu dictates whether
81+ the stage is enabled.
82+
83+ To perform updates with an aggregation, use one of
84+ these stages:
85+
86+ .. include:: /includes/aggregation/update-aggregation-stages.rst
87+
88+ .. step:: Fill in your aggregation stage.
89+
90+ Fill in your stage with the appropriate values.
91+ If :ref:`Comment Mode <pipeline-settings>` is enabled, the pipeline
92+ builder provides syntactic guidelines for your selected stage.
93+
94+ As you modify your stage, Atlas updates the preview documents on
95+ the right based on the results of the current stage.
96+
97+ For examples of what you might include in your aggregation stage,
98+ see the :ref:`examples <agg-updates-examples>` on this page.
99+
100+ Add stages as needed. For more information on creating aggregation
101+ pipelines in Atlas, refer to :ref:`agg-pipeline-create`.
102+
103+ .. step:: Export the aggregation pipeline.
104+
105+ .. procedure::
106+ :style: connected
107+
108+ .. step:: Click Export to Language.
109+
110+ You can find this button at the top of the pipeline builder.
111+
112+ .. step:: Select your desired export language.
113+
114+ In the :guilabel:`Export Pipeline To` menu, select your desired
115+ language.
116+
117+ The :guilabel:`My Pipeline` pane on the left displays your
118+ pipeline in |mdb-shell| syntax. You can copy this directly to execute
119+ your pipeline in the |mdb-shell|.
120+
121+ The pane on the right displays your pipeline in the selected
122+ language. Select your preferred language.
123+
124+ .. step:: Select options, if desired.
125+
126+ *(Optional)*: Check the :guilabel:`Include Import Statements` option
127+ to include the required import statements for the language selected.
128+
129+ *(Optional)*: Check the :guilabel:`Include Driver Syntax` option
130+ to include Driver-specific code to:
131+
132+ - Initialize the client
133+ - Specify the database and collection
134+ - Perform the aggregation operation
135+
136+ .. step:: Copy the pipeline.
137+
138+ Click the :guilabel:`Copy` button at the top-right of the pipeline
139+ to copy the pipeline for the selected language to your clipboard.
140+ Paste the copied pipeline into your application.
141+
142+ .. _agg-updates-examples:
143+
144+ Examples
145+ --------
146+
147+ The following examples demonstrate how to use the aggregation pipeline
148+ stages ``$set``, ``$replaceRoot``, and ``$addFields`` to perform updates.
149+
150+ updateOne with $set
151+ ~~~~~~~~~~~~~~~~~~~
33152
34153Create an example ``students`` collection (if the collection does
35154not currently exist, insert operations will create the collection):
@@ -68,8 +187,8 @@ To verify the update, you can query the collection:
68187
69188 db.students.find().pretty()
70189
71- Example 2
72- ----------
190+ updateMany with $replaceRoot and $set
191+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73192
74193Create an example ``students2`` collection (if the collection does not
75194currently exist, insert operations will create the collection):
@@ -125,8 +244,8 @@ To verify the update, you can query the collection:
125244
126245 db.students2.find()
127246
128- Example 3
129- ---------
247+ updateMany with $set
248+ ~~~~~~~~~~~~~~~~~~~~
130249
131250Create an example ``students3`` collection (if the collection does not
132251currently exist, insert operations will create the collection):
@@ -186,8 +305,8 @@ To verify the update, you can query the collection:
186305
187306 db.students3.find()
188307
189- Example 4
190- ---------
308+ updateOne with $set
309+ ~~~~~~~~~~~~~~~~~~~
191310
192311Create an example ``students4`` collection (if the collection does
193312not currently exist, insert operations will create the collection):
@@ -223,8 +342,8 @@ To verify the update, query the collection:
223342
224343 db.students4.find()
225344
226- Example 5
227- ---------
345+ updateMany with $addFields
346+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
228347
229348Create an example ``temperatures`` collection that contains
230349temperatures in Celsius (if the collection does not currently exist,
@@ -276,7 +395,7 @@ To verify the update, you can query the collection:
276395 db.temperatures.find()
277396
278397Additional Examples
279- -------------------
398+ ~~~~~~~~~~~~~~~~~~~
280399
281400See also the various update method pages for additional examples:
282401
0 commit comments