@@ -16,13 +16,15 @@ Overview
16
16
In this guide, you can learn how to use bulk operations in the
17
17
MongoDB Java Driver.
18
18
19
- .. note:: Improved Bulk Write Commands
19
+ .. note:: Client Bulk Write Method
20
20
21
- The first half of this guide focuses on the ``MongoCollection.bulkWrite()``
22
- method. In {+mdb-server+} version 8.0 and {+driver-short+} version 5.3 and later,
23
- you can use an improved ``MongoClient.bulkWrite()`` method which can write
24
- to multiple databases and collections at once. See the :ref:`improved-bulk-write`
25
- section below to learn how to use the new method.
21
+ This guide primarily focuses on the ``MongoCollection.bulkWrite()``
22
+ method, which allows you to perform bulk operations on a collection.
23
+ When connecting to a deployment running {+mdb-server+} 8.0 or later,
24
+ you can use the ``MongoClient.bulkWrite()`` method to write
25
+ to multiple databases and collections in the same call. See the
26
+ :ref:`java-sync-client-bulk-write` section below to learn how to use the
27
+ ``MongoClient.bulkWrite()`` method.
26
28
27
29
To perform a create, replace, update, or delete operation,
28
30
use its corresponding method. For example, to insert one document,
@@ -108,7 +110,7 @@ describing people:
108
110
For more information about the methods and classes mentioned in this section,
109
111
see the `InsertOneModel
110
112
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__
111
- API Documentation .
113
+ API documentation .
112
114
113
115
Replace Operation
114
116
~~~~~~~~~~~~~~~~~
@@ -140,7 +142,7 @@ contains an added ``location`` field:
140
142
For more information about the methods and classes mentioned in this section,
141
143
see the following resources:
142
144
143
- - `ReplaceOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__ API Documentation
145
+ - `ReplaceOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__ API documentation
144
146
- :manual:`Unique indexes </core/index-unique/>` Server Manual Explanation
145
147
146
148
Update Operation
@@ -176,8 +178,8 @@ the ``age`` field in a document where the ``_id`` is ``2``:
176
178
For more information about the methods and classes mentioned in this section,
177
179
see the following resources:
178
180
179
- - `UpdateOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__ API Documentation
180
- - `UpdateManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__ API Documentation
181
+ - `UpdateOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__ API documentation
182
+ - `UpdateManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__ API documentation
181
183
- :manual:`unique indexes </core/index-unique/>` Server Manual Explanation
182
184
183
185
Delete Operation
@@ -210,7 +212,7 @@ a document where the ``_id`` is ``1``:
210
212
:end-before: end deleteDocumentsExample
211
213
212
214
For more information about the methods and classes mentioned in this section,
213
- see the following API Documentation :
215
+ see the following API documentation :
214
216
215
217
- `DeleteOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOneModel.html>`__
216
218
- `DeleteManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteManyModel.html>`__
@@ -296,45 +298,48 @@ operations to execute in any order:
296
298
{ "_id": 6, "name": "Zaynab Omar", "age": 37 }
297
299
298
300
For more information about the methods and classes mentioned in this section,
299
- see the following API Documentation :
301
+ see the following API documentation :
300
302
301
303
- `BulkWriteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html>`__
302
304
- `ordered() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html#ordered(boolean)>`__
303
305
304
- .. _improved -bulk-write:
306
+ .. _java-sync-client -bulk-write:
305
307
306
- Improved Bulk Write Command
307
- ---------------------------
308
+ Client Bulk Write Method
309
+ ------------------------
308
310
309
- Starting with {+mdb-server+} version 8.0 and {+driver-short+} version 5.3, there
310
- is an improved bulk write method, ``MongoClient.bulkWrite()``, that writes to
311
- different databases and collections in the same cluster.
311
+ When connecting to a deployment running {+mdb-server+} 8.0 or later,
312
+ you can use the ``MongoClient.bulkWrite()`` method to write
313
+ to multiple databases and collections in the same cluster.
312
314
313
- In the previous examples on this page, the ``bulkWrite()`` method takes a list
314
- of ``WriteModel`` documents in which the specified subclass of ``WriteModel``
315
- represents the corresponding write operation. For example, ``InsertOneModel``
316
- represents inserting one document.
315
+ In the examples in preceding sections of this page, the ``MongoCollection. bulkWrite()`` method
316
+ takes a list of ``WriteModel`` documents in which the specified subclass of
317
+ ``WriteModel`` represents the corresponding write operation. For example, an
318
+ instance of ``InsertOneModel`` represents an operation to insert one document.
317
319
318
- Similarly, the new ``bulkWrite()`` method takes a
319
- list of ``ClientNamespacedWriteModel`` objects to represent the write operation.
320
+ .. TODO - not necessarily instances
321
+
322
+ Similarly, the ``MongoClient.bulkWrite()`` method takes a
323
+ list of ``ClientNamespacedWriteModel`` instances to represent different write operations.
320
324
However, you do not need to specify the subclass that represents the corresponding
321
325
write operation. Instead, the ``ClientNamespacedWriteModel`` object contains different
322
326
methods to represent different write operations, such as ``insertOne()`` or
323
327
``updateOne()``.
324
328
325
329
These methods take a ``MongoNamespace`` object that defines which
326
- database and collection to write to, and a ``Document`` object that defines the
327
- document information. Some methods, such as ``updateOne()`` and ``replaceOne()``,
328
- also take a ``Filters`` object that defines the filter for the operation.
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 filter for the operation.
329
333
330
- The following sections describe how to use the new ``bulkWrite()`` method.
334
+ The following sections describe how to use the client ``bulkWrite()`` method.
331
335
332
336
Insert Example
333
337
~~~~~~~~~~~~~~
334
338
335
- The following example shows how to use the new ``bulkWrite()`` method to insert
336
- two documents. One document is inserted into the ``people`` collection in our
337
- database. The other document is inserted into a collection called ``things``.
339
+ This following example shows how to use the ``bulkWrite()`` method to insert
340
+ two documents. One document is inserted into the ``people`` collection in a
341
+ database named ``db``. The other document is inserted into a collection called ``things``
342
+ in the ``db`` database.
338
343
We use the ``MongoNamespace`` object to define the database and collection we
339
344
want each write operation to be applied to.
340
345
@@ -348,18 +353,13 @@ want each write operation to be applied to.
348
353
ClientNamespacedWriteModel.insertOne(thingsNamespace, new Document("object", "washing machine"))
349
354
));
350
355
351
- After this example is executed, the ``people`` collection holds a
352
- ``{ "name": "Julia Smith" }`` document and the ``things`` collection holds
353
- a ``{ "object": "washing machine" }`` document.
354
-
355
356
.. TODO: link documentation
356
357
357
358
Replace Example
358
359
~~~~~~~~~~~~~~~
359
360
360
- The following example shows how to use the ``bulkWrite()`` method to replace an
361
- existing document in the ``people`` collection and an existing document in the
362
- ``things`` collection.
361
+ The following example shows how to use the ``bulkWrite()`` method to replace
362
+ existing documents in the ``people`` and ``things`` collections.
363
363
364
364
.. code-block:: java
365
365
@@ -379,28 +379,32 @@ existing document in the ``people`` collection and an existing document in the
379
379
)
380
380
));
381
381
382
- After this example is executed, the document in the ``people`` collection in
383
- which the ``_id`` field has the value of ``1`` has been replaced with a new
384
- ``{ "name": "Maximus Farquaad" }`` document. Also, the document
385
- in the ``things`` collection in which the ``_id`` field has the value of ``1``
386
- has been replaced with a ``{ "object": "potato" }`` document.
382
+ After this example runs successfully, the document in the ``people`` collection in
383
+ which the ``_id`` field has the value of ``1`` is replaced with a new
384
+ document. The document in the ``things`` collection in which the
385
+ ``_id`` field has the value of ``1`` is replaced with a new document.
386
+
387
+ .. _java-sync-client-bulk-write-options:
387
388
388
389
Bulk Write Options
389
390
~~~~~~~~~~~~~~~~~~
390
391
391
- Similarly to the :ref:`orderOfExecution` section above, you can use the
392
- ``ClientBulkWriteOptions`` object to execute the new ``bulkWrite()`` method
393
- with options.
392
+ .. TODO: reword this
393
+
394
+ You can use the ``ClientBulkWriteOptions`` object to execute the ``bulkWrite()``
395
+ method with options.
394
396
395
397
Order of Execution Example
396
398
``````````````````````````
399
+ By default, the write operations execute in the order that you specify them. However,
400
+ you can pass ``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions``
401
+ object to perform write operations in an unordered way. When using the unordered
402
+ option, an error-producing operation will not block execution of other bulk
403
+ write operations.
397
404
398
- As described in the previous Order of Execution section, by default the write
399
- operations execute in the order they're specified. However, we can pass
400
- ``false`` to the ``ordered()`` method on the ``ClientBulkWriteOptions`` object
401
- to execute write operations in any order.
405
+ .. TODO: the object itself is not necessarily being created
402
406
403
- The following code shows how to use the ``ordered()`` method on the
407
+ The following code shows how to set the ``ordered()`` method on the
404
408
``ClientBulkWriteOptions`` object.
405
409
406
410
.. code-block:: java
@@ -409,11 +413,13 @@ The following code shows how to use the ``ordered()`` method on the
409
413
410
414
ClientBulkWriteOptions options = new ClientBulkWriteOptions().ordered(false);
411
415
412
- ClientBulkWriteResult result = client.bulkWrite(List<>(
413
- ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Donkey Kong")),
414
- ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Mario"))
416
+ ClientBulkWriteResult result = client.bulkWrite(
417
+ List<>(
418
+ ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Donkey Kong")),
419
+ ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Mario"))
415
420
),
416
- options);
421
+ options
422
+ );
417
423
418
424
.. TODO: link documentation
419
425
@@ -455,5 +461,5 @@ options for how the command is executed.
455
461
.. OPEN QUESTIONS FOR ENGINEERING:
456
462
.. Should I include the extraneous types like the Client...Result object?
457
463
.. Should I include the different ClientNamespacedInsertModel... etc?
458
- .. Is ClientWriteModel being exposed for public use?
459
- .. Is MongoCollection.bulkWrite() being deprecated?
464
+ .. Is ClientWriteModel being exposed for public use? <no>
465
+ .. Is MongoCollection.bulkWrite() being deprecated? <no>
0 commit comments