@@ -9,23 +9,142 @@ Updates with Aggregation Pipeline
9
9
.. meta::
10
10
:description: The following page provides examples of updates with aggregation pipelines.
11
11
12
+ .. contents:: On this page
13
+ :local:
14
+ :backlinks: none
15
+ :depth: 2
16
+ :class: singlecol
17
+
12
18
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:
15
27
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
22
29
23
30
Using the aggregation pipeline allows for a more expressive update
24
31
statement, such as expressing conditional updates based on current
25
32
field values or updating one field using the value of another field(s).
26
33
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
+ ~~~~~~~~~~~~~~~~~~~
29
148
30
149
Create an example ``students`` collection (if the collection does
31
150
not currently exist, insert operations will create the collection):
@@ -64,8 +183,8 @@ To verify the update, you can query the collection:
64
183
65
184
db.students.find().pretty()
66
185
67
- Example 2
68
- ----------
186
+ updateMany with $replaceRoot and $set
187
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
188
70
189
Create an example ``students2`` collection (if the collection does not
71
190
currently exist, insert operations will create the collection):
@@ -121,8 +240,8 @@ To verify the update, you can query the collection:
121
240
122
241
db.students2.find()
123
242
124
- Example 3
125
- ---------
243
+ updateMany with $set
244
+ ~~~~~~~~~~~~~~~~~~~~
126
245
127
246
Create an example ``students3`` collection (if the collection does not
128
247
currently exist, insert operations will create the collection):
@@ -182,8 +301,8 @@ To verify the update, you can query the collection:
182
301
183
302
db.students3.find()
184
303
185
- Example 4
186
- ---------
304
+ updateOne with $set
305
+ ~~~~~~~~~~~~~~~~~~~
187
306
188
307
Create an example ``students4`` collection (if the collection does
189
308
not currently exist, insert operations will create the collection):
@@ -219,8 +338,8 @@ To verify the update, query the collection:
219
338
220
339
db.students4.find()
221
340
222
- Example 5
223
- ---------
341
+ updateMany with $addFields
342
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
224
343
225
344
Create an example ``temperatures`` collection that contains
226
345
temperatures in Celsius (if the collection does not currently exist,
@@ -272,7 +391,7 @@ To verify the update, you can query the collection:
272
391
db.temperatures.find()
273
392
274
393
Additional Examples
275
- -------------------
394
+ ~~~~~~~~~~~~~~~~~~~
276
395
277
396
See also the various update method pages for additional examples:
278
397
0 commit comments