@@ -313,18 +313,16 @@ you can use the ``MongoClient.bulkWrite()`` method to write
313
313
to multiple databases and collections in the same cluster.
314
314
315
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
316
+ takes a list of ``WriteModel`` instances in which the specified subclass of
317
317
``WriteModel`` represents the corresponding write operation. For example, an
318
318
instance of ``InsertOneModel`` represents an operation to insert one document.
319
319
320
- .. TODO - not necessarily instances
321
-
322
320
Similarly, the ``MongoClient.bulkWrite()`` method takes a
323
- list of ``ClientNamespacedWriteModel`` instances to represent different write operations.
321
+ list of ``ClientNamespacedWriteModel`` instances to represent different write operations.
324
322
However, you do not need to specify the subclass that represents the corresponding
325
323
write operation. Instead, the ``ClientNamespacedWriteModel`` object contains different
326
- methods to represent different write operations, such as ``insertOne()`` or
327
- ``updateOne()``.
324
+ methods to represent different write operations, such as ``ClientNamespacedWriteModel. insertOne()`` or
325
+ ``ClientNamespacedWriteModel. updateOne()``.
328
326
329
327
These methods take a ``MongoNamespace`` object that defines which
330
328
database and collection to write to, and a ``Document`` object that defines
@@ -348,10 +346,12 @@ want each write operation to be applied to.
348
346
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
349
347
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
350
348
351
- ClientBulkWriteResult result = client.bulkWrite(List<>(
352
- ClientNamespacedWriteModel.insertOne(peopleNamespace, new Document("name", "Julia Smith")),
353
- ClientNamespacedWriteModel.insertOne(thingsNamespace, new Document("object", "washing machine"))
354
- ));
349
+ List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
350
+
351
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(peopleNamespace, new Document("name", "Julia Smith")));
352
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(thingsNamespace, new Document("object", "washing machine")));
353
+
354
+ ClientBulkWriteResult result = client.bulkWrite(bulkOperations);
355
355
356
356
.. TODO: link documentation
357
357
@@ -366,18 +366,23 @@ existing documents in the ``people`` and ``things`` collections.
366
366
MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
367
367
MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
368
368
369
- ClientBulkWriteResult result = client.bulkWrite(List<>(
370
- ClientNamespacedWriteModel.replaceOne(
369
+ List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
370
+
371
+ bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
371
372
peopleNamespace,
372
373
Filters.eq("_id", 1),
373
374
new Document("name", "Maximus Farquaad")
374
- ),
375
- ClientNamespacedWriteModel.replaceOne(
375
+ )
376
+ );
377
+
378
+ bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
376
379
thingsNamespace,
377
380
Filters.eq("_id", 1),
378
381
new Document("object", "potato")
379
382
)
380
- ));
383
+ );
384
+
385
+ ClientBulkWriteResult result = client.bulkWrite(bulkOperations);
381
386
382
387
After this example runs successfully, the document in the ``people`` collection in
383
388
which the ``_id`` field has the value of ``1`` is replaced with a new
@@ -389,37 +394,41 @@ document. The document in the ``things`` collection in which the
389
394
Bulk Write Options
390
395
~~~~~~~~~~~~~~~~~~
391
396
392
- .. TODO: reword this
393
-
394
- You can use the ``ClientBulkWriteOptions`` object to execute the ``bulkWrite()``
395
- method with options.
397
+ You can use the ``ClientBulkWriteOptions`` interface to specify options when
398
+ running the ``bulkWrite()`` method.
396
399
397
400
Order of Execution Example
398
401
``````````````````````````
402
+
399
403
By default, the write operations execute in the order that you specify them. However,
400
404
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
405
+ interface to perform write operations in an unordered way. When using the unordered
402
406
option, an error-producing operation will not block execution of other bulk
403
407
write operations.
404
408
405
- .. TODO: the object itself is not necessarily being created
406
-
407
- The following code shows how to set the ``ordered()`` method on the
408
- ``ClientBulkWriteOptions`` object.
409
+ The following code shows how to set the ``ordered()`` method when creating an
410
+ instance of ``ClientBulkWriteOptions``.
409
411
410
412
.. code-block:: java
411
413
412
414
MongoNamespace namespace = new MongoNamespace("db", "people");
413
415
414
416
ClientBulkWriteOptions options = new ClientBulkWriteOptions().ordered(false);
415
417
416
- ClientBulkWriteResult result = client.bulkWrite(
417
- List<>(
418
- ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Donkey Kong")),
419
- ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Mario"))
420
- ),
421
- options
422
- );
418
+ List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
419
+
420
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Donkey Kong)));
421
+
422
+ // duplicate key
423
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("_id", 1).append("name", "Mario")));
424
+
425
+ bulkOperations.add(ClientNamespacedWriteModel.insertOne(namespace, new Document("name", "Princess Peach")));
426
+
427
+ ClientBulkWriteResult result = client.bulkWrite(bulkOperations, options);
428
+
429
+ Even though the write operation inserting a document with a duplicate key will
430
+ result in an error, the other operations will be executed because we have specified
431
+ the ``ordered()`` option to be ``false``.
423
432
424
433
.. TODO: link documentation
425
434
@@ -456,10 +465,6 @@ This method uses the ``ClientNamespacedWriteModel`` and its methods
456
465
The method can also take a ``ClientBulkWriteOptions`` object to specify different
457
466
options for how the command is executed.
458
467
459
- .. TODO: Add API Documentation
468
+ .. TODO: insert server documentation
460
469
461
- .. OPEN QUESTIONS FOR ENGINEERING:
462
- .. Should I include the extraneous types like the Client...Result object?
463
- .. Should I include the different ClientNamespacedInsertModel... etc?
464
- .. Is ClientWriteModel being exposed for public use? <no>
465
- .. Is MongoCollection.bulkWrite() being deprecated? <no>
470
+ .. TODO: Add API Documentation
0 commit comments