@@ -20,11 +20,14 @@ Bulk Write Operations
20
20
Overview
21
21
--------
22
22
23
+ This guide shows you how to use the {+driver-short+} to perform **bulk write operations**
24
+ that include multiple write operations in a single database call.
25
+
23
26
Consider a scenario in which you want to insert a document into a collection,
24
27
update multiple other documents, then delete a document. If you use
25
- individual methods, each operation requires its own database call. This guide
26
- shows you how to use bulk write operations to perform multiple write operations
27
- in a single database call.
28
+ individual methods, each operation requires its own database call. If you use
29
+ a bulk write operation, you can improve efficiency by performing multiple write
30
+ operations in a single database call.
28
31
29
32
Sample Data
30
33
~~~~~~~~~~~
@@ -51,11 +54,10 @@ The following sections show how to create and use instances of the preceding cla
51
54
52
55
.. tip:: Bulk Write Operations with POCOs
53
56
54
- The examples in this guide use ``BsonDocument`` as the type for ``TDocument``.
55
- You can also use a Plain Old CLR Object (POCO) as the type for ``TDocument``.
56
-
57
- To use a POCO, define a class that represents the documents in your collection.
58
- The class must have properties that match the fields in your documents.
57
+ The examples in this guide use the ``BsonDocument`` type for the ``TDocument`` type
58
+ in all generic classes. You can also use a Plain Old CLR Object (POCO) for these
59
+ classes. To do so, you must define a class that represents the documents in your
60
+ collection. The class must have properties that match the fields in your documents.
59
61
For more information, see :ref:`<csharp-poco>`.
60
62
61
63
Insert Operations
@@ -164,11 +166,11 @@ parameters:
164
166
| **Data Type:** `IEnumerable<ArrayFilterDefinition> <https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=net-8.0>`__
165
167
| **Default:** ``null``
166
168
167
- The following example creates an instance of the ``BulkWriteUpdateOneModel<TDocument >``
168
- class, and uses ``BsonDocument`` as the type for ``TDocument``. The
169
- filter matches the first document in the ``sample_restaurants.restaurants`` collection
170
- where the value of the ``name`` field is ``"Mongo's Deli"``. The update operation sets
171
- the value of the ``cuisine`` field to ``"Sandwiches and Salads"``.
169
+ In the following code example, the ``BulkWriteUpdateOneModel<BsonDocument >`` object
170
+ represents an update operation on the ``sample_restaurants.restaurants`` collection.
171
+ The operation matches the first document in the collection where the value of the ``name``
172
+ field is ``"Mongo's Deli"``. It then updates the value of the ``cuisine`` field in the
173
+ matched document to ``"Sandwiches and Salads"``.
172
174
173
175
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
174
176
:start-after: start-bulk-update-one
@@ -347,9 +349,10 @@ Run the Write Operations
347
349
------------------------
348
350
349
351
After you define a ``BulkWriteModel`` instance for each operation that you want to perform,
350
- pass an ``IReadOnlyList`` collection of these instances to the ``BulkWrite()`` or
351
- ``BulkWriteAsync()`` method. By default, these methods run the operations in the order
352
- they're defined in the collection.
352
+ create an instance of a class that implements the ``IReadOnlyList`` interface. Add your
353
+ ``BulkWriteModel`` objects to this ``IReadOnlyList``, then pass the ``IReadOnlyList``
354
+ to the ``BulkWrite()`` or ``BulkWriteAsync()`` method. By default, these methods run
355
+ the operations in the order they're defined in the collection.
353
356
354
357
.. tip:: IReadOnlyList
355
358
@@ -388,7 +391,7 @@ The preceding code examples produce the following output:
388
391
389
392
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2, 'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []}, acknowledged=True)
390
393
391
- .. tip:: Troubleshooting Bulk Write Operations
394
+ .. tip::
392
395
393
396
If any of the write operations fail, the {+driver-short+} raises a
394
397
``BulkWriteException`` and does not perform any further operations. You can examine
0 commit comments