@@ -19,11 +19,11 @@ MongoDB Java Driver.
19
19
To perform a single create, replace, update, or delete operation, you can use
20
20
the corresponding method. For example, to insert one document and replace one
21
21
document, you can use the ``insertOne()`` and ``replaceOne()`` methods. In this
22
- case, the ``MongoClient`` makes a call to the database for each operation.
23
- You can reduce the number of calls to one by using bulk write operations .
22
+ case, the ``MongoClient`` makes a call to the database for each operation. However,
23
+ by using bulk write operations, you can reduce the number of calls.
24
24
25
25
You can perform bulk write operations by using the bulk write API in the
26
- {+driver-short+} to perform multiple data changes in one call. You can
26
+ {+driver-short+} to define multiple data changes in one method call. You can
27
27
perform bulk operations at the following levels:
28
28
29
29
- :ref:`Collection Level <java-sync-coll-bulk-write>`: You can use the
@@ -320,19 +320,25 @@ instance of ``InsertOneModel`` represents an operation to insert one document.
320
320
321
321
Similarly, the ``MongoClient.bulkWrite()`` method takes a
322
322
list of ``ClientNamespacedWriteModel`` instances to represent different write operations.
323
- Instead of selecting a specific subclass for each write operation, the
324
- ``ClientNamespacedWriteModel`` object contains different methods to represent
325
- write operations. ``ClientNamespacedWriteModel`` contains the methods
326
- ``insertOne()``, ``updateOne()``, ``updateMany()``, ``replaceOne()``,
327
- ``deleteOne()``, and ``deleteMany()``.
323
+ For example, an instance of ``ClientNamespacedInsertOneModel`` represents an
324
+ operation to insert one document.
325
+
326
+ You can construct these ``ClientNamespacedWriteModel`` instances using different
327
+ methods to represent write operations. ``ClientNamespacedWriteModel`` contains
328
+ the methods ``insertOne()``, ``updateOne()``, ``updateMany()``, ``replaceOne()``,
329
+ ``deleteOne()``, and ``deleteMany()``. These methods are used to construct corresponding
330
+ write models. For example, ``ClientNamespacedWriteModel.updateOne()`` is used to
331
+ construct a ``ClientNamespacedUpdateOneModel`` instance, which represents an
332
+ update operation.
328
333
329
334
These methods take a ``MongoNamespace`` object that defines which
330
- database and collection to write to, and a ``Document`` object that defines
331
- information about the write operation. Some methods, such as ``updateOne()`` and
332
- ``replaceOne ()``, also take a ``Filters`` object that defines the subset of
333
- documents to be updated or replaced.
335
+ database and collection to write to. Some, such as ``insertOne()``, can take a
336
+ ``Document`` instance that defines information about the write operation.
337
+ Some other methods, such as ``updateOne ()`` and ``replaceOne()``, take a filter
338
+ object that defines the subset of documents to be updated or replaced.
334
339
335
- The following sections describe how to use the client ``bulkWrite()`` method.
340
+ The following sections provide examples of how to use the client ``bulkWrite()``
341
+ method.
336
342
337
343
Insert Example
338
344
~~~~~~~~~~~~~~
@@ -350,19 +356,20 @@ each write operation applies to.
350
356
351
357
List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
352
358
353
- bulkOperations.add(
354
- ClientNamespacedWriteModel .insertOne(
359
+ ClientNamespacedInsertOneModel insertDocument1 = new ClientNamespacedWriteModel
360
+ .insertOne(
355
361
peopleNamespace,
356
362
new Document("name", "Julia Smith")
357
- )
358
- );
363
+ );
359
364
360
- bulkOperations.add(
361
- ClientNamespacedWriteModel .insertOne(
365
+ ClientNamespacedInsertOneModel insertDocument2 = new ClientNamespacedWriteModel
366
+ .insertOne(
362
367
thingsNamespace,
363
368
new Document("object", "washing machine")
364
- )
365
- );
369
+ );
370
+
371
+ bulkOperations.add(insertDocument1);
372
+ bulkOperations.add(insertDocument2);
366
373
367
374
ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations);
368
375
@@ -413,12 +420,13 @@ method to specify options when running the bulk write operations.
413
420
Order of Execution Example
414
421
``````````````````````````
415
422
416
- By default, the driver performs write operations in the order that you specify them until
417
- an error occurs, or until they execute successfully. However, you can pass
418
- ``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions``
419
- interface to perform write operations in an unordered way. When using the unordered
420
- option, an error-producing operation does not block execution of other write
421
- operations in the call to the ``bulkWrite()`` method.
423
+ By default, the individual operations in a bulk operation are executed in the
424
+ order that you specify them until an error occurs, or until they execute
425
+ successfully. However, you can pass ``false`` to the ``ordered()`` method on
426
+ the ``ClientBulkWriteOptions`` interface to perform write operations in an
427
+ unordered way. When using the unordered option, an error-producing operation
428
+ does not prevent execution of other write operations in the call to the
429
+ ``bulkWrite()`` method.
422
430
423
431
The following code sets the ``ordered()`` method on an
424
432
instance of ``ClientBulkWriteOptions`` and performs a bulk write operation to
@@ -469,9 +477,9 @@ Summary
469
477
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
470
478
471
479
To perform a bulk operation, you create and pass a list of
472
- ``WriteModel`` documents to the ``bulkWrite()`` method.
480
+ ``WriteModel`` instances to the ``bulkWrite()`` method.
473
481
474
- There are 6 different ``WriteModel`` documents : ``InsertOneModel``,
482
+ There are 6 different ``WriteModel`` subtypes : ``InsertOneModel``,
475
483
``ReplaceOneModel``, ``UpdateOneModel``, ``UpdateManyModel``,
476
484
``DeleteOneModel`` and ``DeleteManyModel``.
477
485
@@ -488,12 +496,20 @@ When connecting to a deployment running {+mdb-server+} version 8.0 or later, you
488
496
can use the ``MongoClient.bulkWrite()`` method to perform bulk operations on multiple
489
497
databases and collections at once.
490
498
491
- This method uses the ``ClientNamespacedWriteModel`` and its methods
492
- ``insertOne()``, ``updateOne()``, ``updateMany()``, ``replaceOne()``,
493
- ``deleteOne()``, and ``deleteMany()``.
499
+ To perform a client bulk operation, you create an pass a list of
500
+ ``ClientNamespacedWriteModel`` instances to this method.
501
+
502
+ There are six subtypes of ``ClientNamespacedWriteModel``: ``ClientNamespacedInsertOneModel``,
503
+ ``ClientNamespacedUpdateOneModel``, ``ClientNamespacedUpdateManyModel``,
504
+ ``ClientNamespacedReplaceOneModel``, ``ClientNamespacedDeleteOneModel``, and
505
+ ``ClientNamespacedDeleteManyModel``. To construct these write models, you can
506
+ use the ``ClientNamespacedWriteModel`` methods ``insertOne()``, ``updateOne()``,
507
+ ``updateMany()``, ``replaceOne()``, ``deleteOne()``, and ``deleteMany()``. These
508
+ methods take a ``MongoNamespace`` object that defines which
509
+ database and collection to write to.
494
510
495
- The method can also take a ``ClientBulkWriteOptions`` object to specify different
496
- options for how the command is executed.
511
+ The ``MongoClient.bulkWrite()`` method can also take a ``ClientBulkWriteOptions``
512
+ object to specify different options for how the command is executed.
497
513
498
514
To learn more about the client ``bulkWrite`` command, see the
499
515
:manual:`bulkWrite() <reference/command/bulkWrite/>` method reference in the {+mdb-server+}
0 commit comments