Skip to content

Commit 9ebc298

Browse files
committed
wip
1 parent b44cdfc commit 9ebc298

File tree

3 files changed

+143
-19
lines changed

3 files changed

+143
-19
lines changed

source/fundamentals/crud/write-operations/update.txt

+75-19
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The following sections describe these methods in more detail.
4444
Many of the methods in this guide have multiple overloads. The examples
4545
in this guide use the simplest overload for demonstration purposes. For
4646
more information about the available overloads, see the
47-
:manual:`{+new-api-root+} API documentation </api-reference>`.
47+
`API documentation. {+new-api-root+}/index.html>`__
4848

4949
.. tip:: Interactive Lab
5050

@@ -73,7 +73,7 @@ methods all accept the following parameters:
7373
- An instance of the ``FilterDefinition`` class that specifies the documents to update.
7474
To learn how to create a query filter, see :ref:`csharp-specify-query`.
7575

76-
**Data Type:** `FilterDefinition<TDocument> <{+new-api-root+>/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__
76+
**Data Type:** `FilterDefinition <{+new-api-root+>/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__
7777

7878
* - ``update``
7979
- An instance of the ``UpdateDefinition`` class. This object specifies the kind of update
@@ -99,8 +99,8 @@ Update Multiple Values
9999
----------------------
100100

101101
The ``UpdateOne()``, ``UpdateOneAsync()``, ``UpdateMany()``, and ``UpdateManyAsync()``
102-
methods each accept one ``UpdateDefinition`` object. The following sections describe how
103-
to multiple values in a single method call.
102+
methods each accept only one ``UpdateDefinition`` object. The following sections describe how
103+
to update multiple values in a single method call.
104104

105105
Combine Update Definitions
106106
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -124,13 +124,13 @@ The ``Combine()`` method returns a single ``UpdateDefinition`` object that defin
124124
multiple update operations.
125125

126126
The following code example uses the ``Combine()`` method to combine a
127-
:manual:`$set </reference/operator/update/set/#mongodb-update-up.-set>` operation and a
128-
:manual:`$pop </reference/operator/update/pop/#mongodb-update-up.-pop>`
127+
:manual:`$set </reference/operator/update/set/#mongodb-update-up.-set>` operation and an
128+
:manual:`$unset </reference/operator/update/pop/#mongodb-update-up.-unset>`
129129
operation:
130130

131131
.. tabs::
132132

133-
.. tab:: UpdateOne (Synchronous)
133+
.. tab:: UpdateOne (Sync)
134134
:tabid: update-one-sync
135135

136136
.. literalinclude:: /includes/code-examples/update-one/UpdateOne.cs
@@ -140,7 +140,7 @@ operation:
140140
:start-after: // start-combine-sync
141141
:end-before: // end-combine-sync
142142

143-
.. tab:: UpdateOne (Asynchronous)
143+
.. tab:: UpdateOne (Async)
144144
:tabid: update-one-async
145145

146146
.. literalinclude:: /includes/code-examples/update-one/UpdateOne.cs
@@ -150,7 +150,7 @@ operation:
150150
:start-after: // start-combine-async
151151
:end-before: // end-combine-async
152152

153-
.. tab:: UpdateMany (Synchronous)
153+
.. tab:: UpdateMany (Sync)
154154
:tabid: update-many-sync
155155

156156
.. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
@@ -160,7 +160,7 @@ operation:
160160
:start-after: // start-combine-sync
161161
:end-before: // end-combine-sync
162162

163-
.. tab:: UpdateMany (Asynchronous)
163+
.. tab:: UpdateMany (Async)
164164
:tabid: update-many-async
165165

166166
.. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
@@ -173,8 +173,12 @@ operation:
173173
Update Pipelines
174174
~~~~~~~~~~~~~~~~
175175

176-
Builders.Update.Pipeline is used to create an update pipeline for MongoDB update operations. An update pipeline allows you to specify a sequence of update stages that modify the documents in a collection. Each stage in the pipeline is an update operation that transforms the document in some way. This is similar to the aggregation pipeline but is used specifically for updates.
177-
This method accepts the following parameter:
176+
If your application connects to {+mdb-server+} 4.2 or later, you can join
177+
a sequence of update operations into a single
178+
:manual:`aggregation pipeline. </core/aggregation-pipeline/>`
179+
180+
To create an update pipeline, call the ``Builders.Update.Pipeline()`` method. This method
181+
accepts the following parameter:
178182

179183
.. list-table::
180184
:widths: 30 70
@@ -184,17 +188,69 @@ This method accepts the following parameter:
184188
- Description
185189

186190
* - ``pipeline``
187-
- A ``PipelineDefinition`` instance that specifies the update pipeline.
191+
- A ``PipelineDefinition`` instance that represents the update pipeline. To create
192+
a ``PipelineDefinition`` object, create a BSON object for each update operation you
193+
want to perform, then pass these objects to the ``Pipeline.Create()`` method.
188194

189195
**Data Type:** ``PipelineDefinition<TDocument, TDocument>``
190196

191-
.. tip:: Aggregation Pipelines in Update Operations
197+
The ``Pipeline()`` method returns a single ``UpdateDefinition`` object that defines
198+
multiple aggregation stages.
199+
200+
The following code example uses the ``Pipeline()`` method to combine a
201+
:manual:`$set </reference/operator/update/set/#mongodb-update-up.-set>` operation and an
202+
:manual:`$unset </reference/operator/update/pop/#mongodb-update-up.-unset>`
203+
operation:
204+
205+
.. tabs::
206+
207+
.. tab:: UpdateOne (Sync)
208+
:tabid: update-one-sync
209+
210+
.. literalinclude:: /includes/code-examples/update-one/UpdateOne.cs
211+
:language: csharp
212+
:copyable: true
213+
:dedent:
214+
:start-after: // start-pipeline-sync
215+
:end-before: // end-pipeline-sync
216+
217+
.. tab:: UpdateOne (Async)
218+
:tabid: update-one-async
219+
220+
.. literalinclude:: /includes/code-examples/update-one/UpdateOne.cs
221+
:language: csharp
222+
:copyable: true
223+
:dedent:
224+
:start-after: // start-pipeline-async
225+
:end-before: // end-pipeline-async
226+
227+
.. tab:: UpdateMany (Sync)
228+
:tabid: update-many-sync
229+
230+
.. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
231+
:language: csharp
232+
:copyable: true
233+
:dedent:
234+
:start-after: // start-pipeline-sync
235+
:end-before: // end-pipeline-sync
236+
237+
.. tab:: UpdateMany (Async)
238+
:tabid: update-many-async
239+
240+
.. literalinclude:: /includes/code-examples/update-many/UpdateMany.cs
241+
:language: csharp
242+
:copyable: true
243+
:dedent:
244+
:start-after: // start-pipeline-async
245+
:end-before: // end-pipeline-async
246+
247+
.. note:: Unsupported Operations
192248

193-
If you are using MongoDB Version 4.2 or later, you can use aggregation
194-
pipelines made up of a subset of aggregation stages in update operations. For
195-
more information on the aggregation stages MongoDB supports in
196-
aggregation pipelines used in update operations, see the tutorial about building
197-
:manual:`updates with aggregation pipelines </tutorial/update-documents-with-aggregation-pipeline/>`.
249+
Update pipelines don't support all update operations, but they do support certain
250+
aggregation stages not found in other update definitions. For a list of
251+
update operations supported by pipelines, see
252+
:manual:`Updates with Aggregation Pipeline </tutorial/update-documents-with-aggregation-pipeline/>`
253+
in the {+mdb-server+} manual.
198254

199255
.. _csharp-update-options:
200256

source/includes/code-examples/update-many/UpdateMany.cs

+34
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,40 @@ public static async Task CombineUpdatesAsync()
127127
await _restaurantsCollection.UpdateManyAsync(filter, combinedUpdate);
128128
// end-combine-async
129129
}
130+
public static void PipelineUpdate()
131+
{
132+
// start-pipeline-sync
133+
var filter = Builders<Restaurant>.Filter
134+
.Eq("name", "Downtown Deli");
135+
136+
var updatePipeline = Builders<Restaurant>.Update.Pipeline(
137+
PipelineDefinition<Restaurant, Restaurant>.Create(
138+
new BsonDocument("$set", new BsonDocument("cuisine", "French")),
139+
new BsonDocument("$unset", "borough")
140+
)
141+
);
142+
143+
_restaurantsCollection.UpdateMany(filter, updatePipeline);
144+
// end-pipeline-sync
145+
}
146+
147+
public static async Task PipelineUpdateAsync()
148+
{
149+
// start-pipeline-async
150+
var filter = Builders<Restaurant>.Filter
151+
.Eq("name", "Downtown Deli");
152+
153+
var updatePipeline = Builders<Restaurant>.Update.Pipeline(
154+
PipelineDefinition<Restaurant, Restaurant>.Create(
155+
new BsonDocument("$set", new BsonDocument("cuisine", "French")),
156+
new BsonDocument("$unset", "borough")
157+
)
158+
);
159+
160+
await _restaurantsCollection.UpdateManyAsync(filter, updatePipeline);
161+
// end-pipeline-async
162+
}
163+
130164
}
131165

132166
public class Restaurant

source/includes/code-examples/update-one/UpdateOne.cs

+34
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,40 @@ public static async Task CombineUpdatesAsync()
121121
// end-combine-async
122122
}
123123

124+
public static void PipelineUpdate()
125+
{
126+
// start-pipeline-sync
127+
var filter = Builders<Restaurant>.Filter
128+
.Eq("name", "Downtown Deli");
129+
130+
var updatePipeline = Builders<Restaurant>.Update.Pipeline(
131+
PipelineDefinition<Restaurant, Restaurant>.Create(
132+
new BsonDocument("$set", new BsonDocument("cuisine", "French")),
133+
new BsonDocument("$unset", "borough")
134+
)
135+
);
136+
137+
_restaurantsCollection.UpdateOne(filter, updatePipeline);
138+
// end-pipeline-sync
139+
}
140+
141+
public static async Task PipelineUpdateAsync()
142+
{
143+
// start-pipeline-async
144+
var filter = Builders<Restaurant>.Filter
145+
.Eq("name", "Downtown Deli");
146+
147+
var updatePipeline = Builders<Restaurant>.Update.Pipeline(
148+
PipelineDefinition<Restaurant, Restaurant>.Create(
149+
new BsonDocument("$set", new BsonDocument("cuisine", "French")),
150+
new BsonDocument("$unset", "borough")
151+
)
152+
);
153+
154+
await _restaurantsCollection.UpdateOneAsync(filter, updatePipeline);
155+
// end-pipeline-async
156+
}
157+
124158
private static void Setup()
125159
{
126160
// Allows automapping of the camelCase database fields to models

0 commit comments

Comments
 (0)