@@ -16,7 +16,7 @@ Overview
1616--------
1717
1818In this guide, you can learn how to use bulk operations in the
19- MongoDB Java Driver.
19+ MongoDB Kotlin Driver.
2020
2121To perform a create, replace, update, or delete operation,
2222use its corresponding method. For example, to insert one document,
@@ -44,10 +44,15 @@ in a collection:
4444 { "_id": 1 }
4545 { "_id": 2 }
4646
47+ This data is modeled with the following Kotlin data class:
48+
49+ .. literalinclude:: /examples/generated/BulkTest.snippet.bulk-data-model.kt
50+ :language: kotlin
51+
4752For more information about the methods and classes mentioned in this section,
4853see the following API Documentation:
4954
50- - `bulkWrite() <{+api+}/apidocs/ mongodb-driver-sync/com/ mongodb/client/MongoCollection.html#bulkWrite(java.util.List, com.mongodb.client.model.BulkWriteOptions) >`__
55+ - `bulkWrite() <{+api+}mongodb-driver-kotlin-coroutine/ mongodb-driver-kotlin-coroutine/ com.mongodb.kotlin. client.coroutine/-mongo-collection/bulk-write.html >`__
5156- `WriteModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/WriteModel.html>`__
5257- `BulkWriteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html>`__
5358
@@ -64,11 +69,8 @@ Example
6469The following example creates an ``InsertOneModel`` for two documents
6570where the ``_id`` values are "3" and "4":
6671
67- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
68- :language: java
69- :dedent:
70- :start-after: begin insertDocumentsExample
71- :end-before: end insertDocumentsExample
72+ .. literalinclude:: /examples/generated/BulkTest.snippet.insert-one.kt
73+ :language: kotlin
7274
7375.. important::
7476
@@ -79,29 +81,24 @@ where the ``_id`` values are "3" and "4":
7981 The following example tries to insert two documents where the ``_id`` is
8082 "1" and "3":
8183
82- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
83- :language: java
84- :dedent:
85- :start-after: begin insertExceptionExample
86- :end-before: end insertExceptionExample
84+ .. io-code-block::
8785
88- The following shows the output of the preceding code:
89-
90- .. code-block:: shell
91- :copyable: false
86+ .. input:: /examples/generated/BulkTest.snippet.bulk-write-exception.kt
87+ :language: kotlin
9288
93- A MongoBulkWriteException occurred with the following message:
94- Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
95- Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
96- error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
89+ .. output::
90+ :language: console
91+
92+ A MongoBulkWriteException occurred with the following message:
93+ Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017.
94+ Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key
95+ error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
9796
9897 To see why the document with the ``_id`` of "3" didn't insert, see
9998 the :ref:`Order of Execution <orderOfExecution>` section.
10099
101100For more information about the methods and classes mentioned in this section,
102- see the `InsertOneModel
103- <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__
104- API Documentation.
101+ see the `InsertOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/InsertOneModel.html>`__ API Documentation.
105102
106103Replace Operation
107104~~~~~~~~~~~~~~~~~
@@ -124,11 +121,8 @@ The following example creates a ``ReplaceOneModel`` to
124121replace a document where the ``_id`` is "1" with a document that
125122contains an additional field:
126123
127- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
128- :language: java
129- :dedent:
130- :start-after: begin replaceDocumentsExample
131- :end-before: end replaceDocumentsExample
124+ .. literalinclude:: /examples/generated/BulkTest.snippet.replace-one.kt
125+ :language: kotlin
132126
133127For more information about the methods and classes mentioned in this section,
134128see the following resources:
@@ -161,11 +155,8 @@ The following example creates an ``UpdateOneModel`` to update
161155a document where the ``_id`` is "2" to a document that
162156contains an additional field:
163157
164- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
158+ .. literalinclude:: /examples/generated/BulkTest.snippet.update-one.kt
165159 :language: java
166- :dedent:
167- :start-after: begin updateDocumentsExample
168- :end-before: end updateDocumentsExample
169160
170161For more information about the methods and classes mentioned in this section,
171162see the following resources:
@@ -197,11 +188,8 @@ Example
197188The following example creates a ``DeleteOneModel`` to delete
198189a document where the ``_id`` is "1":
199190
200- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
201- :language: java
202- :dedent:
203- :start-after: begin deleteDocumentsExample
204- :end-before: end deleteDocumentsExample
191+ .. literalinclude:: /examples/generated/BulkTest.snippet.delete.kt
192+ :language: kotlin
205193
206194For more information about the methods and classes mentioned in this section,
207195see the following API Documentation:
@@ -235,11 +223,8 @@ The following example performs these bulk operations:
235223- An update operation for a document where the ``_id`` is "3" to a document that contains an additional field
236224- A delete operation for all documents that contain the field ``x`` with the value "2"
237225
238- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
239- :language: java
240- :dedent:
241- :start-after: begin bulkWriteExample
242- :end-before: end bulkWriteExample
226+ .. literalinclude:: /examples/generated/BulkTest.snippet.ordered.kt
227+ :language: kotlin
243228
244229After running this example, your collection contains the following
245230document:
@@ -260,11 +245,8 @@ the bulk operation reports them at the end.
260245Adding to the preceding example, including the following specifies the bulk
261246operations to execute in any order:
262247
263- .. literalinclude:: /includes/fundamentals/code-snippets/BulkWrite.java
264- :language: java
265- :dedent:
266- :start-after: begin bulkWriteNotOrderedExample
267- :end-before: end bulkWriteNotOrderedExample
248+ .. literalinclude:: /examples/generated/BulkTest.snippet.unordered.kt
249+ :language: kotlin
268250
269251.. note::
270252
0 commit comments