Skip to content

Commit 84a3a8a

Browse files
committed
first draft
1 parent cb693cf commit 84a3a8a

File tree

2 files changed

+270
-113
lines changed

2 files changed

+270
-113
lines changed

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

Lines changed: 181 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ constructor accepts the following parameters:
259259
| **Data Type:** {+bool-data-type+}
260260
| **Default:** ``false``
261261

262-
The following example creates an instance of the ``BulkWriteReplaceOneyModel<TDocument>``
262+
The following example creates an instance of the ``BulkWriteReplaceOneModel<TDocument>``
263263
class, and uses ``BsonDocument`` as the type for ``TDocument``. The
264264
filter matches the document in the ``sample_restaurants.restaurants`` collection
265265
where the value of the ``restaurant_id`` field is ``"1234"``. The replace operation
@@ -281,12 +281,49 @@ removes all fields from this document and sets new values in the ``name``, ``cui
281281
Delete Operations
282282
~~~~~~~~~~~~~~~~~
283283

284-
To delete a document, create an instance of the ``BulkWriteDeleteOneModel`` and pass in
285-
a query filter that specifies the document that you want to delete. The
286-
``BulkWriteDeleteOneModel`` operation removes
287-
only the *first document* that matches your query filter.
284+
To delete a document, create an instance of the ``BulkWriteDeleteOneModel<TDocument>``
285+
class. The ``BulkWriteDeleteOneModel<TDocument>`` constructor accepts the following
286+
parameters:
287+
288+
.. list-table::
289+
:header-rows: 1
290+
:stub-columns: 1
291+
292+
* - Parameter
293+
- Description
294+
295+
* - ``collectionNamespace``
296+
- | The database and collection to insert the BSON document into.
297+
|
298+
| **Data Type:** {+string-data-type+} or `CollectionNamespace <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.CollectionNamespace.html>`__
299+
300+
* - ``filter``
301+
- | The **query filter** that specifies the criteria used to match documents in your collection.
302+
| The ``DeleteOne`` operation deletes *only the first document* that matches the
303+
| query filter.
304+
|
305+
| **Data Type:** `FilterDefinition<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__
288306

289-
The following example creates an instance of the ``BulkWriteDeleteOneModel`` class:
307+
* - ``collation``
308+
- | *Optional.* The language collation to use when sorting results. See
309+
| :manual:`the delete statements</reference/command/delete/#std-label-deletes-array-collation>`
310+
for more information.
311+
|
312+
| **Data Type:** `Collation <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Collation.html>`__
313+
| **Default:** ``null``
314+
315+
* - ``hint``
316+
- | *Optional.* The index to use to scan for documents.
317+
See :manual:`the MongoDB server manual</reference/command/update/#std-label-update-command-hint>`
318+
for more information.
319+
|
320+
| **Data Type:** `BsonValue <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonValue.html>`__
321+
| **Default:** ``null``
322+
323+
The following example creates an instance of the ``BulkWriteDeleteOneModel<TDocument>``
324+
class, and uses ``BsonDocument`` as the type for ``TDocument``. The
325+
filter matches the document in the ``sample_restaurants.restaurants`` collection
326+
where the value of the ``restaurant_id`` field is ``"5678"``.
290327

291328
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
292329
:start-after: start-bulk-delete-one
@@ -295,12 +332,15 @@ The following example creates an instance of the ``BulkWriteDeleteOneModel`` cla
295332
:copyable:
296333
:dedent: 8
297334

298-
To delete multiple documents, create an instance of the ``BulkWriteDeleteManyModel``
335+
To delete multiple documents, create an instance of the ``BulkWriteDeleteManyModel<TDocument>``
299336
class and pass a query filter that specifies the document that you want to delete.
300-
The ``BulkWriteDeleteManyModel`` operation removes *all* documents that match your
337+
The ``DeleteMany`` operation removes *all documents* that match your
301338
query filter.
302339

303-
The following example creates an instance of the ``BulkWriteDeleteManyModel`` class:
340+
The following example creates an instance of the ``BulkWriteDeleteManyModel<TDocument>``
341+
class, and uses ``BsonDocument`` as the type for ``TDocument``. The
342+
filter matches the document in the ``sample_restaurants.restaurants`` collection
343+
where the value of the ``name`` field is ``"Mongo's Deli"``.
304344

305345
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
306346
:start-after: start-bulk-delete-many
@@ -313,45 +353,58 @@ Call the ``BulkWrite()`` Method
313353
--------------------------------
314354

315355
After you define a ``BulkWriteModel`` instance for each operation that you want to perform,
316-
pass an ``IList`` of these instances to the ``BulkWrite()`` or ``BulkWriteAsync()`` method.
317-
By default, these methods run the operations in the order
318-
they're defined in the list.
356+
pass an ``IReadOnlyList`` collection of these instances to the ``BulkWrite()`` or
357+
``BulkWriteAsync()`` method. By default, these methods run the operations in the order
358+
they're defined in the collection.
359+
360+
.. tip:: IReadOnlyList
361+
362+
``Array`` and ``List`` are two common classes that implement the ``IReadOnlyList``
363+
interface.
319364

320365
The following code examples show how to use the asynchronous
321366
``BulkWriteAsync()`` method and the synchronous ``BulkWrite()`` method to
322367
perform multiple write operations.
323368

324-
.. io-code-block::
369+
.. tabs::
325370

326-
.. input:: /includes/fundamentals/code-examples/BulkWrite.cs
327-
:start-after: start-bulk-write-mixed
328-
:end-before: end-bulk-write-mixed
329-
:language: csharp
330-
:dedent: 8
371+
.. tab:: Asynchronous
372+
:tabid: bulk-write-async
331373

332-
.. output::
333-
334-
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2, 'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []}, acknowledged=True)
374+
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
375+
:start-after: start-bulk-write-async
376+
:end-before: end-bulk-write-async
377+
:language: csharp
378+
:copyable:
379+
:dedent: 8
335380

336-
If any of the write operations fail, the {+driver-short+} raises a
337-
``BulkWriteException`` and does not perform any further operations.
338-
``BulkWriteException`` provides a ``details`` attribute that includes the operation
339-
that failed, and details about the exception.
381+
.. tab:: Synchronous
382+
:tabid: bulk-write-sync
383+
384+
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
385+
:start-after: start-bulk-write-async
386+
:end-before: end-bulk-write-async
387+
:language: csharp
388+
:copyable:
389+
:dedent: 8
340390

341-
.. note::
391+
The preceding code examples produce the following output:
392+
393+
.. code-block:: shell
394+
395+
BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2, 'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []}, acknowledged=True)
342396

343-
When the {+driver-short+} executes a bulk write operation, it uses the
344-
``WriteConcern`` of the
345-
collection on which the operation is running. The driver reports all write
346-
concern errors after attempting all operations, regardless of execution order.
397+
If any of the write operations fail, the {+driver-short+} raises a
398+
``BulkWriteException`` and does not perform any further operations. You can examine
399+
the properties of the exception to determine which operation failed.
347400

348401
Customize Bulk Write Operations
349402
-------------------------------
350403

351-
The ``bulk_write()`` method optionally accepts additional
352-
parameters, which represent options you can use to configure the bulk write
353-
operation. If you don't specify any additional options, the driver does not customize
354-
the bulk write operation.
404+
When you call the ``BulkWrite()`` or ``BulkWriteAsync()`` method, you can pass an
405+
instance of the ``ClientBulkWriteOptions`` class. The ``ClientBulkWriteOptions`` class
406+
contains the following properties, which represent options you can use to configure the
407+
bulk write operation:
355408

356409
.. list-table::
357410
:widths: 30 70
@@ -360,60 +413,84 @@ the bulk write operation.
360413
* - Property
361414
- Description
362415

363-
* - ``ordered``
364-
- | If ``True``, the driver performs the write operations in the order
365-
provided. If an error occurs, the remaining operations are not
366-
attempted.
367-
|
368-
| If ``False``, the driver performs the operations in an
369-
arbitrary order and attempts to perform all operations.
370-
| Defaults to ``True``.
371-
372-
* - ``bypass_document_validation``
416+
* - ``BypassDocumentValidation``
373417
- | Specifies whether the operation bypasses document-level validation. For more
374418
information, see :manual:`Schema
375-
Validation </core/schema-validation/#schema-validation>` in the MongoDB
376-
Server manual.
377-
| Defaults to ``False``.
419+
Validation </core/schema-validation/#schema-validation>` in the {+mdb-server+}
420+
manual.
421+
| Defaults to ``false``.
378422

379-
* - ``session``
380-
- | An instance of ``ClientSession``. For more information, see the `API
381-
documentation <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__.
382-
383-
* - ``comment``
384-
- | A comment to attach to the operation. For more information, see the :manual:`delete command
423+
* - ``Comment``
424+
- | A comment to attach to the operation, in the form of a ``BsonValue``. For
425+
more information, see the :manual:`delete command
385426
fields </reference/command/delete/#command-fields>` guide in the
386427
{+mdb-server+} manual.
387428

388-
* - ``let``
389-
- | A map of parameter names and values. Values must be constant or closed
429+
* - ``IsOrdered``
430+
- | If ``true``, the driver performs the write operations in the order
431+
provided. If an error occurs, the remaining operations are not
432+
attempted.
433+
|
434+
| If ``false``, the driver performs the operations in an
435+
arbitrary order and attempts to perform all operations.
436+
| Defaults to ``True``.
437+
438+
* - ``Let``
439+
- | A map of parameter names and values, in the form of a ``BsonDocument``. Values
440+
must be constant or closed
390441
expressions that don't reference document fields. For more information,
391442
see the :manual:`let statement
392443
</reference/command/delete/#std-label-delete-let-syntax>` in the
393444
{+mdb-server+} manual.
445+
446+
* - ``VerboseResult``
447+
- | Specifies whether the ``ClientBulkWriteResult`` object returned by the operation
448+
includes detailed results for each successful write operation.
449+
| Defaults to ``false``.
394450

395-
The following example calls the ``bulk_write()`` method from the preceding example, with the ``ordered`` option set
396-
to ``False``:
451+
* - ``WriteConcern``
452+
- | The write concern to use for the write operation, as a value from the ``WriteConcern``
453+
enum.
454+
| Defaults to the write concern of the collection on which the operation is running.
397455

398-
.. literalinclude:: /includes/write/bulk-write.py
399-
:start-after: start-bulk-write-unordered
400-
:end-before: end-bulk-write-unordered
401-
:language: python
402-
:copyable:
456+
The following code examples use a ``ClientBulkWriteOptions`` object to customize
457+
a delete operation:
458+
459+
.. tabs::
460+
461+
.. tab:: Asynchronous
462+
:tabid: bulk-write-options-async
403463

404-
If any of the write operations in an unordered bulk write fail, {+driver-short+}
405-
reports the errors only after attempting all operations.
464+
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
465+
:start-after: start-bulk-write-options-async
466+
:end-before: end-bulk-write-options-async
467+
:language: csharp
468+
:copyable:
469+
:dedent: 8
406470

407-
.. note::
471+
.. tab:: Synchronous
472+
:tabid: bulk-write-options-sync
473+
474+
.. literalinclude:: /includes/fundamentals/code-examples/BulkWrite.cs
475+
:start-after: start-bulk-write-options-async
476+
:end-before: end-bulk-write-options-async
477+
:language: csharp
478+
:copyable:
479+
:dedent: 8
480+
481+
.. note:: Unordered Bulk Write Operations
408482

409483
Unordered bulk operations do not guarantee order of execution. The order can
410484
differ from the way you list them to optimize the runtime.
485+
486+
If any of the write operations in an unordered bulk write fail, the {+driver-short+}
487+
reports the errors only after attempting all operations.
411488

412489
Return Value
413490
------------
414491

415-
The ``bulk_write()`` method returns a ``BulkWriteResult`` object. The
416-
``BulkWriteResult`` object contains the following properties:
492+
The ``BulkWrite()`` and ``BulkWriteAsync()`` methods return a ``ClientBulkWriteResult``
493+
object that contains the following properties:
417494

418495
.. list-table::
419496
:widths: 30 70
@@ -422,53 +499,58 @@ The ``bulk_write()`` method returns a ``BulkWriteResult`` object. The
422499
* - Property
423500
- Description
424501

425-
* - ``acknowledged``
426-
- | Indicates if the server acknowledged the write operation.
502+
* - ``Acknowledged``
503+
- | Indicates whether the server acknowledged the bulk write operation.
427504

428-
* - ``bulk_api_result``
429-
- | The raw bulk API result returned by the server.
430-
431-
* - ``deleted_count``
505+
* - ``DeleteResults``
506+
- | An ``IReadOnlyDictionary<int, BulkWriteDeleteResult>`` object containing the
507+
results of each successful delete operation, if any.
508+
509+
* - ``DeletedCount``
432510
- | The number of documents deleted, if any.
433511

434-
* - ``inserted_count``
512+
* - ``InsertResults``
513+
- | An ``IReadOnlyDictionary<int, BulkWriteInsertOneResult>`` object containing the
514+
results of each successful insert operation, if any.
515+
516+
* - ``InsertedCount``
435517
- | The number of documents inserted, if any.
436518

437-
* - ``matched_count``
519+
* - ``MatchedCount``
438520
- | The number of documents matched for an update, if applicable.
439521

440-
* - ``modified_count``
522+
* - ``ModifiedCount``
441523
- | The number of documents modified, if any.
442524

443-
* - ``upserted_count``
525+
* - ``UpsertResults``
526+
- | An ``IReadOnlyDictionary<int, BulkWriteUpdateResult>`` object containing the
527+
results of each successful update operation, if any.
528+
529+
* - ``UpsertedCount``
444530
- | The number of documents upserted, if any.
445531

446-
* - ``upserted_ids``
447-
- | A map of the operation's index to the ``_id`` of the upserted documents, if
448-
applicable.
449-
450532
Additional Information
451533
----------------------
452534

453535
To learn how to perform individual write operations, see the following guides:
454536

455-
- :ref:`pymongo-write-insert`
456-
- :ref:`pymongo-write-update`
457-
- :ref:`pymongo-write-replace`
458-
- :ref:`pymongo-write-delete`
459-
460-
API Documentation
461-
~~~~~~~~~~~~~~~~~
537+
- :ref:`csharp-change-guide`
538+
- :ref:`csharp-insert-guide`
539+
- :ref:`csharp-delete-guide`
462540

463541
To learn more about any of the methods or types discussed in this
464-
guide, see the following API Documentation:
465-
466-
- `bulk_write() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.bulk_write>`__
467-
- `InsertOne <{+api-root+}pymongo/operations.html#pymongo.operations.InsertOne>`__
468-
- `UpdateOne <{+api-root+}pymongo/operations.html#pymongo.operations.UpdateOne>`__
469-
- `UpdateMany <{+api-root+}pymongo/operations.html#pymongo.operations.UpdateMany>`__
470-
- `ReplaceOne <{+api-root+}pymongo/operations.html#pymongo.operations.ReplaceOne>`__
471-
- `DeleteOne <{+api-root+}pymongo/operations.html#pymongo.operations.DeleteOne>`__
472-
- `DeleteMany <{+api-root+}pymongo/operations.html#pymongo.operations.DeleteMany>`__
473-
- `BulkWriteResult <{+api-root+}pymongo/results.html#pymongo.results.BulkWriteResult>`__
474-
- `BulkWriteError <{+api-root+}pymongo/errors.html#pymongo.errors.BulkWriteError>`__
542+
guide, see the following API documentation:
543+
544+
- `BulkWrite() <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWrite.html>`__
545+
- `BulkWriteAsync() <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteAsync.html>`__
546+
- `ClientBulkWriteOptions <{+new-api-root+}/MongoDB.Driver/MongoClient.ClientBulkWriteOptions.html>`__
547+
- `ClientBulkWriteResult <{+new-api-root+}/MongoDB.Driver/MongoClient.ClientBulkWriteResult.html>`__
548+
- `BulkWriteInsertOneModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteInsertOneModel-1.html>`__
549+
- `BulkWriteUpdateOneModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteUpdateOneModel-1.html>`__
550+
- `BulkWriteUpdateManyModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteUpdateManyModel-1.html>`__
551+
- `BulkWriteReplaceOneModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteReplaceOneModel-1.html>`__
552+
- `BulkWriteDeleteOneModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteDeleteOneModel-1.html>`__
553+
- `BulkWriteDeleteManyModel <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteDeleteManyModel-1.html>`__
554+
- `BulkWriteInsertOneResult <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteInsertOneResult.html>`__
555+
- `BulkWriteUpdateResult <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteUpdateResult.html>`__
556+
- `BulkWriteDeleteResult <{+new-api-root+}/MongoDB.Driver/MongoClient.BulkWriteDeleteResult.html>`__

0 commit comments

Comments
 (0)