@@ -21,40 +21,45 @@ MongoDB Java Driver.
21
21
22
22
To perform a single create, replace, update, or delete operation, you can use
23
23
the corresponding method. For example, to insert one document and replace one
24
- document, you can use the ``insertOne()`` and ``replaceOne()`` methods. In this
25
- case , the ``MongoClient`` makes a call to the database for each operation.
24
+ document, you can use the ``insertOne()`` and ``replaceOne()`` methods. When you
25
+ use these methods , the ``MongoClient`` makes one call to the database for each operation.
26
26
27
- Generally, you can reduce the number of calls to the database by using bulk write
28
- operations . You can perform bulk write operations at the following levels:
27
+ By using a bulk write operation, you can perform multiple write operations in
28
+ fewer database calls . You can perform bulk write operations at the following levels:
29
29
30
- - :ref:`Collection Level <java-sync-coll-bulk-write>`: You can use the
30
+ - :ref:`Collection <java-sync-coll-bulk-write>`: You can use the
31
31
``MongoCollection.bulkWrite()`` method to perform bulk write operations on a
32
32
single collection. This method groups write operations together by the kind
33
33
of operation. For example, ``MongoCollection.bulkWrite()`` puts multiple update
34
- operations in the same call, but makes two calls to the database for an insert
34
+ operations in one call, but makes two separate calls to the database for an insert
35
35
operation and a replace operation.
36
36
37
- - :ref:`Client Level <java-sync-client-bulk-write>`: When running {+mdb-server+}
38
- version 8.0 or later, you can use the ``MongoClient.bulkWrite()`` method to perform
39
- bulk write operations on multiple collections and databases in the same cluster.
40
- This method groups multiple kinds of write operations into one call.
37
+ - :ref:`Client <java-sync-client-bulk-write>`: If your application connects to
38
+ {+mdb-server+} version 8.0 or later, you can use the ``MongoClient.bulkWrite()``
39
+ method to perform bulk write operations on multiple collections and databases
40
+ in the same cluster. This method performs all write operations
41
+ in one database call.
41
42
42
43
.. _java-sync-coll-bulk-write:
43
44
44
45
Collection Bulk Write
45
46
---------------------
46
47
47
- Bulk operations consist of many write operations. To perform a bulk operation
48
- at the collection level, pass a ``List`` of ``WriteModel`` documents to the
49
- ``MongoCollection.bulkWrite()`` method. A ``WriteModel`` is a model that
50
- represents any of the write operations .
48
+ Bulk write operations contain one or more write operations. To perform a bulk
49
+ write operation at the collection level, pass a ``List`` of ``WriteModel``
50
+ documents to the ``MongoCollection.bulkWrite()`` method. A ``WriteModel`` is a
51
+ model that represents a write operation .
51
52
52
- The ``MongoCollection.bulkWrite()`` method splits operations of different kinds into
53
- different batches. For example, when you pass ``DeleteOneModel``,
54
- ``DeleteManyModel``, and ``ReplaceOneModel`` operations to the method, the method
55
- splits the delete operations into one batch and the replace operation
56
- into another. During this process, the client might reorder operations for
57
- efficiency if the bulk operation is not ordered.
53
+ The ``MongoCollection.bulkWrite()`` method performs each kind of write
54
+ operations in a separate call. For example, when you pass ``DeleteOneModel``,
55
+ ``DeleteManyModel``, and ``ReplaceOneModel`` objects to the method, it creates
56
+ two calls: one for the delete operations and one for the replace operation.
57
+
58
+ .. note::
59
+
60
+ When the client splits operations into calls, it might reorder operations for
61
+ efficiency if the bulk write operation is not ordered. To learn more about
62
+ operation execution order, see the :ref:`orderOfExecution` section.
58
63
59
64
The following sections show how to create and use each ``WriteModel``
60
65
document. The examples in each section use the following documents in the
@@ -324,43 +329,91 @@ Client Bulk Write
324
329
When connecting to a deployment running {+mdb-server+} 8.0 or later,
325
330
you can use the ``MongoClient.bulkWrite()`` method to write
326
331
to multiple databases and collections in the same cluster. The ``MongoClient.bulkWrite()``
327
- method does not split different kinds of operations into different batches .
332
+ method performs all write operations in a single call .
328
333
329
- In the examples in preceding sections of this page, the ``MongoCollection.bulkWrite()`` method
330
- takes a list of ``WriteModel`` instances in which the specified subclass of
331
- ``WriteModel`` represents the corresponding write operation. For example, an
332
- instance of ``InsertOneModel`` represents an operation to insert one document.
333
-
334
- Similarly, the ``MongoClient.bulkWrite()`` method takes a
334
+ The ``MongoClient.bulkWrite()`` method takes a
335
335
list of ``ClientNamespacedWriteModel`` instances to represent different write operations.
336
336
For example, an instance of ``ClientNamespacedInsertOneModel`` represents an
337
337
operation to insert one document.
338
338
339
- You can construct instances of ``ClientNamespacedWriteModel`` using the following
340
- methods:
339
+ You can construct instances of the ``ClientNamespacedWriteModel`` interface by using
340
+ instance methods described in the table below. These methods take a
341
+ ``MongoNamespace`` object that defines which database and collection to write to.
342
+
343
+ .. list-table::
344
+ :header-rows: 1
345
+
346
+ * - Instance Method
347
+ - Class Constructed
348
+ - Parameters
349
+ - Operation Description
350
+
351
+ * - ``insertOne()``
352
+ - ``ClientNamespacedInsertOneModel``
353
+ - ``namespace``: defines which database and collection to write to
354
+
355
+ ``document``: defines the document to insert
356
+ - Creates a model to insert a document into the ``namespace``.
357
+
358
+ * - ``updateOne()``
359
+ - ``ClientNamespacedInsertOneModel``
360
+ - ``namespace``: defines which database and collection to write to
361
+
362
+ ``filter``: defines filter that selects which document to update
363
+
364
+ ``update``: defines update to apply to matching document
365
+
366
+ ``updatePipeline``: defines update pipeline to apply to matching document
367
+
368
+ ``options``: (optional) defines options to apply when updating document
369
+
370
+ One of ``update`` or ``updatePipeline`` must be specified.
371
+ - Creates a model to update at most one document in the ``namespace``
372
+ that matches ``filter``.
373
+
374
+ * - ``updateMany()``
375
+ - ``ClientNamespacedUpdateManyModel``
376
+ - ``namespace``: defines which database and collection to write to
377
+
378
+ ``filter``: defines filter that selects which documents to update
379
+
380
+ ``update``: defines update to apply to matching documents
381
+
382
+ ``updatePipeline``: defines update pipeline to apply to matching documents
383
+
384
+ ``options``: (optional) defines options to apply when updating documents
385
+
386
+ One of ``update`` or ``updatePipeline`` must be specified.
387
+ - Updates all documents in the ``namespace`` that match ``filter``.
388
+
389
+ * - ``replaceOne()``
390
+ - ``ClientNamespacedReplaceOneModel``
391
+ - ``namespace``: defines which database and collection to write to
392
+
393
+ ``filter``: defines filter that selects which document to replace
341
394
342
- - ``insertOne()``
395
+ ``replacement``: defines replacement document
343
396
344
- - ``updateOne()``
397
+ ``options``: (optional) defines options to apply when replacing documents
398
+ - Replaces at most one document in the ``namespace`` that matches ``filter``.
345
399
346
- - ``updateMany()``
400
+ * - ``deleteOne()``
401
+ - ``ClientNamespacedDeleteOneModel``
402
+ - ``namespace``: defines which database and collection to write to
347
403
348
- - ``replaceOne()``
404
+ ``filter``: defines filter that selects which document to delete
349
405
350
- - ``deleteOne()``
406
+ ``option``: (optional) defines options to apply when deleting document
407
+ - Deletes at most one document in the ``namespace`` that matches ``filter``.
351
408
352
- - ``deleteMany()``
409
+ * - ``deleteMany()``
410
+ - ``ClientNamespacedDeleteManyModel``
411
+ - ``namespace``: defines which database and collection to write to
353
412
354
- These methods are used to construct corresponding write models.
355
- For example, ``ClientNamespacedWriteModel.updateOne()`` is used to
356
- construct a ``ClientNamespacedUpdateOneModel`` instance, which represents an
357
- update operation.
413
+ ``filter``: defines filter that selects which documents to delete
358
414
359
- These methods take a ``MongoNamespace`` object that defines which
360
- database and collection to write to. Some, such as ``insertOne()``, can take a
361
- ``Document`` instance that defines information about the write operation.
362
- Some other methods, such as ``updateOne()`` and ``replaceOne()``, take a filter
363
- object that defines the subset of documents to be updated or replaced.
415
+ ``option``: (optional) defines options to apply when deleting documents
416
+ - Deletes all documents in the ``namespace`` that match ``filter``.
364
417
365
418
The following sections provide examples of how to use the client ``bulkWrite()``
366
419
method.
0 commit comments