@@ -313,18 +313,16 @@ you can use the ``MongoClient.bulkWrite()`` method to write
313313to multiple databases and collections in the same cluster.
314314
315315In 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
317317``WriteModel`` represents the corresponding write operation. For example, an
318318instance of ``InsertOneModel`` represents an operation to insert one document.
319319
320- .. TODO - not necessarily instances
321-
322320Similarly, 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.
324322However, you do not need to specify the subclass that represents the corresponding
325323write 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()``.
328326
329327These methods take a ``MongoNamespace`` object that defines which
330328database and collection to write to, and a ``Document`` object that defines
@@ -348,10 +346,12 @@ want each write operation to be applied to.
348346 MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
349347 MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
350348
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);
355355
356356.. TODO: link documentation
357357
@@ -366,18 +366,23 @@ existing documents in the ``people`` and ``things`` collections.
366366 MongoNamespace peopleNamespace = new MongoNamespace("db", "people");
367367 MongoNamespace thingsNamespace = new MongoNamespace("db", "things");
368368
369- ClientBulkWriteResult result = client.bulkWrite(List<>(
370- ClientNamespacedWriteModel.replaceOne(
369+ List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>();
370+
371+ bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
371372 peopleNamespace,
372373 Filters.eq("_id", 1),
373374 new Document("name", "Maximus Farquaad")
374- ),
375- ClientNamespacedWriteModel.replaceOne(
375+ )
376+ );
377+
378+ bulkOperations.add(ClientNamespacedWriteModel.replaceOne(
376379 thingsNamespace,
377380 Filters.eq("_id", 1),
378381 new Document("object", "potato")
379382 )
380- ));
383+ );
384+
385+ ClientBulkWriteResult result = client.bulkWrite(bulkOperations);
381386
382387After this example runs successfully, the document in the ``people`` collection in
383388which 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
389394Bulk Write Options
390395~~~~~~~~~~~~~~~~~~
391396
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.
396399
397400Order of Execution Example
398401``````````````````````````
402+
399403By default, the write operations execute in the order that you specify them. However,
400404you 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
402406option, an error-producing operation will not block execution of other bulk
403407write operations.
404408
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``.
409411
410412.. code-block:: java
411413
412414 MongoNamespace namespace = new MongoNamespace("db", "people");
413415
414416 ClientBulkWriteOptions options = new ClientBulkWriteOptions().ordered(false);
415417
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``.
423432
424433.. TODO: link documentation
425434
@@ -456,10 +465,6 @@ This method uses the ``ClientNamespacedWriteModel`` and its methods
456465The method can also take a ``ClientBulkWriteOptions`` object to specify different
457466options for how the command is executed.
458467
459- .. TODO: Add API Documentation
468+ .. TODO: insert server documentation
460469
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