@@ -50,7 +50,7 @@ The documents in this collection are modeled by the following {+language+} data
50
50
Define the Write Operations
51
51
---------------------------
52
52
53
- For each write operation you want to perform, create a correspinding
53
+ For each write operation you want to perform, create a corresponding
54
54
instance of one of the following operation classes that inherit from the
55
55
generic ``WriteModel`` class:
56
56
@@ -65,50 +65,61 @@ Then, pass a list of these instances to the ``bulkWrite()`` method.
65
65
66
66
The following sections show how to create and use instances of the
67
67
preceding classes. The :ref:`kotlin-sync-bulkwrite-method` section
68
- demonstrates how to pass a list of models to the ``bulkWrite()`` method.
68
+ demonstrates how to pass a list of models to the ``bulkWrite()`` method
69
+ to perform the bulk operation.
69
70
70
71
Insert Operations
71
72
~~~~~~~~~~~~~~~~~
72
73
73
74
To perform an insert operation, create an ``InsertOneModel`` instance and specify
74
75
the document you want to insert.
75
76
76
- The following example creates an instance of ``InsertOne ``:
77
+ The following example creates an instance of ``InsertOneModel ``:
77
78
78
79
.. literalinclude:: /includes/write/bulk.kt
79
80
:start-after: start-bulk-insert-one
80
81
:end-before: end-bulk-insert-one
81
82
:language: kotlin
82
83
:copyable:
83
84
84
- To insert multiple documents, create an instance of ``InsertOne`` for each document.
85
+ To insert multiple documents, create an instance of ``InsertOneModel``
86
+ for each document.
87
+
88
+ .. important::
89
+
90
+ When performing a bulk operation, the ``InsertOneModel`` cannot
91
+ insert a document with an ``_id`` that already exists in the
92
+ collection. In this situation, the driver throws a
93
+ ``MongoBulkWriteException``.
85
94
86
95
Update Operations
87
96
~~~~~~~~~~~~~~~~~
88
97
89
- To update a document, create an instance of ``UpdateOne `` and pass in
98
+ To update a document, create an instance of ``UpdateOneModel `` and pass
90
99
the following arguments:
91
100
92
101
- A **query filter** that specifies the criteria used to match documents in your collection
93
102
- The update operation you want to perform. For more information about update
94
103
operations, see the :manual:`Field Update Operators
95
104
</reference/operator/update-field/>` guide in the {+mdb-server+} manual.
96
105
97
- ``UpdateOne`` updates *the first* document that matches your query filter.
106
+ An ``UpdateOneModel`` instance specifies an update for *the first*
107
+ document that matches your query filter.
98
108
99
- The following example creates an instance of ``UpdateOne ``:
109
+ The following example creates an instance of ``UpdateOneModel ``:
100
110
101
111
.. literalinclude:: /includes/write/bulk.kt
102
112
:start-after: start-bulk-update-one
103
113
:end-before: end-bulk-update-one
104
114
:language: kotlin
105
115
:copyable:
106
116
107
- To update multiple documents, create an instance of ``UpdateMany`` and pass in
108
- the same arguments. ``UpdateMany`` updates *all* documents that match your query
117
+ To update multiple documents, create an instance of ``UpdateManyModel`` and pass
118
+ the same arguments as for ``UpdateOneModel``. The ``UpdateManyModel``
119
+ class specifies updates for *all* documents that match your query
109
120
filter.
110
121
111
- The following example creates an instance of ``UpdateMany ``:
122
+ The following example creates an instance of ``UpdateManyModel ``:
112
123
113
124
.. literalinclude:: /includes/write/bulk.kt
114
125
:start-after: start-bulk-update-many
@@ -120,38 +131,42 @@ Replace Operations
120
131
~~~~~~~~~~~~~~~~~~
121
132
122
133
A replace operation removes all fields and values of a specified document and
123
- replaces them with new ones. To perform a replace operation, create an instance
124
- of ``ReplaceOne`` and pass it a query filter and the fields and values you want
125
- to store in the matching document.
134
+ replaces them with new fields and values that you specify. To perform a
135
+ replace operation, create an instance of ``ReplaceOneModel`` and pass a
136
+ query filter and the fields and values you want to replace the matching
137
+ document with.
126
138
127
- The following example creates an instance of ``ReplaceOne ``:
139
+ The following example creates an instance of ``ReplaceOneModel ``:
128
140
129
141
.. literalinclude:: /includes/write/bulk.kt
130
142
:start-after: start-bulk-replace-one
131
143
:end-before: end-bulk-replace-one
132
144
:language: kotlin
133
145
:copyable:
134
146
135
- To replace multiple documents, you must create an instance of ``ReplaceOne`` for each document.
147
+ To replace multiple documents, you must create an instance of
148
+ ``ReplaceOneModel`` for each document.
136
149
137
150
Delete Operations
138
151
~~~~~~~~~~~~~~~~~
139
152
140
- To delete a document, create an instance of ``DeleteOne`` and pass in a
141
- query filter specifying the document you want to delete. ``DeleteOne`` removes
153
+ To delete a document, create an instance of ``DeleteOneModel`` and pass a
154
+ query filter specifying the document you want to delete. A
155
+ ``DeleteOneModel`` instance provides instructions to delete
142
156
only *the first* document that matches your query filter.
143
157
144
- The following example creates an instance of ``DeleteOne ``:
158
+ The following example creates an instance of ``DeleteOneModel ``:
145
159
146
160
.. literalinclude:: /includes/write/bulk.kt
147
161
:start-after: start-bulk-delete-one
148
162
:end-before: end-bulk-delete-one
149
163
:language: kotlin
150
164
:copyable:
151
165
152
- To delete multiple documents, create an instance of ``DeleteMany`` and pass in a
153
- query filter specifying the document you want to delete. ``DeleteMany`` removes
154
- *all* documents that match your query filter.
166
+ To delete multiple documents, create an instance of ``DeleteManyModel`` and pass a
167
+ query filter specifying the document you want to delete. An instance of
168
+ ``DeleteMany`` provides instructions to remove *all* documents that
169
+ match your query filter.
155
170
156
171
The following example creates an instance of ``DeleteMany``:
157
172
@@ -163,16 +178,16 @@ The following example creates an instance of ``DeleteMany``:
163
178
164
179
.. _kotlin-sync-bulkwrite-method:
165
180
166
- Call the ``bulk_write()`` Method
167
- --------------------------------
181
+ Call the bulkWrite() Method
182
+ ---------------------------
168
183
169
184
After you define a class instance for each operation you want to perform,
170
- pass a list of these instances to the ``bulk_write ()`` method.
185
+ pass a list of these instances to the ``bulkWrite ()`` method.
171
186
By default, the method runs the operations in the order
172
- they're defined in the list.
187
+ that specified by the list of models .
173
188
174
189
The following example performs multiple write operations by using the
175
- ``bulk_write ()`` method:
190
+ ``bulkWrite ()`` method:
176
191
177
192
.. io-code-block::
178
193
@@ -183,7 +198,7 @@ The following example performs multiple write operations by using the
183
198
184
199
.. output::
185
200
186
- BulkWriteResult({'writeErrors': [], 'writeConcernErrors': [], 'nInserted': 2, 'nUpserted': 0, 'nMatched': 2, 'nModified': 2, 'nRemoved': 1, 'upserted': []}, acknowledged=True)
201
+
187
202
188
203
If any of the write operations fail, {+driver-short+} raises a
189
204
``BulkWriteError`` and does not perform any further operations.
@@ -192,17 +207,20 @@ that failed, and details about the exception.
192
207
193
208
.. note::
194
209
195
- When {+ driver-short+} runs a bulk operation, it uses the ``write_concern`` of the
196
- collection in which the operation is running . The driver reports all write
197
- concern errors after attempting all operations, regardless of execution order.
210
+ When the driver runs a bulk operation, it uses the write concern of the
211
+ target collection . The driver reports all write concern errors after
212
+ attempting all operations, regardless of execution order.
198
213
199
214
Customize Bulk Write Operations
200
215
-------------------------------
201
216
202
- The ``bulk_write()`` method optionally accepts additional
203
- parameters, which represent options you can use to configure the bulk write
204
- operation. If you don't specify any additional options, the driver does not customize
205
- the bulk write operation.
217
+ The ``bulkWrite()`` method optionally accepts a parameter which
218
+ specifies options you can use to configure the bulk write
219
+ operation. If you don't specify any options, the driver performs the
220
+ bulk operation with default settings.
221
+
222
+ The following table describes the setter methods that you can use to
223
+ configure a ``BulkWriteOptions`` instance:
206
224
207
225
.. list-table::
208
226
:widths: 30 70
@@ -211,60 +229,54 @@ the bulk write operation.
211
229
* - Property
212
230
- Description
213
231
214
- * - ``ordered``
215
- - | If ``True ``, the driver performs the write operations in the order
232
+ * - ``ordered() ``
233
+ - | If ``true ``, the driver performs the write operations in the order
216
234
provided. If an error occurs, the remaining operations are not
217
235
attempted.
218
236
|
219
- | If ``False ``, the driver performs the operations in an
237
+ | If ``false ``, the driver performs the operations in an
220
238
arbitrary order and attempts to perform all operations.
221
- | Defaults to ``True ``.
239
+ | Defaults to ``true ``.
222
240
223
- * - ``bypass_document_validation``
224
- - | Specifies whether the operation bypasses document-level validation. For more
225
- information, see :manual:`Schema
241
+ * - ``bypassDocumentValidation()``
242
+ - | Specifies whether the update operation bypasses document validation. This lets you
243
+ update documents that don't meet the schema validation requirements, if any
244
+ exist. For more information about schema validation, see :manual:`Schema
226
245
Validation </core/schema-validation/#schema-validation>` in the MongoDB
227
246
Server manual.
228
- | Defaults to ``False ``.
247
+ | Defaults to ``false ``.
229
248
230
- * - ``session``
231
- - | An instance of ``ClientSession``. For more information, see the `API
232
- documentation <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__.
249
+ * - ``comment()``
250
+ - | Sets a comment to attach to the operation.
233
251
234
- * - ``comment ``
235
- - | A comment to attach to the operation. For more information, see the :manual:`delete command
236
- fields </reference/command/delete/#command-fields>` guide in the
237
- {+mdb-server+} manual .
252
+ * - ``let() ``
253
+ - | Provides a map of parameter names and values to set top-level
254
+ variables for the operation. Values must be constant or closed
255
+ expressions that don't reference document fields .
238
256
239
- * - ``let``
240
- - | A map of parameter names and values. Values must be constant or closed
241
- expressions that don't reference document fields. For more information,
242
- see the :manual:`let statement
243
- </reference/command/delete/#std-label-delete-let-syntax>` in the
244
- {+mdb-server+} manual.
245
-
246
- The following example calls the ``bulk_write()`` method from the preceding example, with the ``ordered`` option set
247
- to ``False``:
257
+ The following code creates options and uses the ``ordered()`` method to
258
+ specify an unordered bulk write. Then, the example uses the
259
+ ``bulkWrite()`` method to perform a bulk operation:
248
260
249
261
.. literalinclude:: /includes/write/bulk.kt
250
262
:start-after: start-bulk-write-unordered
251
263
:end-before: end-bulk-write-unordered
252
264
:language: kotlin
253
265
:copyable:
254
266
255
- If any of the write operations in an unordered bulk write fail, {+driver-short+}
267
+ If any of the write operations in an unordered bulk write fail, the {+driver-short+}
256
268
reports the errors only after attempting all operations.
257
269
258
270
.. note::
259
271
260
- Unordered bulk operations do not guarantee order of execution. The order can
272
+ Unordered bulk operations do not guarantee an order of execution. The order can
261
273
differ from the way you list them to optimize the runtime.
262
274
263
275
Return Value
264
276
------------
265
277
266
- The ``bulk_write ()`` method returns a ``BulkWriteResult`` object. The
267
- ``BulkWriteResult`` object contains the following properties :
278
+ The ``bulkWrite ()`` method returns a ``BulkWriteResult`` object. You can
279
+ access the following information from a ``BulkWriteResult`` instance :
268
280
269
281
.. list-table::
270
282
:widths: 30 70
@@ -273,53 +285,48 @@ The ``bulk_write()`` method returns a ``BulkWriteResult`` object. The
273
285
* - Property
274
286
- Description
275
287
276
- * - ``acknowledged ``
288
+ * - ``wasAcknowledged() ``
277
289
- | Indicates if the server acknowledged the write operation.
278
-
279
- * - ``bulk_api_result``
280
- - | The raw bulk API result returned by the server.
281
290
282
- * - ``deleted_count ``
291
+ * - ``getDeletedCount() ``
283
292
- | The number of documents deleted, if any.
284
293
285
- * - ``inserted_count ``
294
+ * - ``getInsertedCount() ``
286
295
- | The number of documents inserted, if any.
287
296
288
- * - ``matched_count``
297
+ * - ``getInserts()``
298
+ - | The list of inserted documents, if any.
299
+
300
+ * - ``getMatchedCount()``
289
301
- | The number of documents matched for an update, if applicable.
290
302
291
- * - ``modified_count ``
303
+ * - ``getModifiedCount() ``
292
304
- | The number of documents modified, if any.
293
305
294
- * - ``upserted_count``
295
- - | The number of documents upserted, if any.
296
-
297
- * - ``upserted_ids``
298
- - | A map of the operation's index to the ``_id`` of the upserted documents, if
299
- applicable.
306
+ * - ``getUpserts()``
307
+ - | The list of upserted documents, if any.
300
308
301
309
Additional Information
302
310
----------------------
303
311
304
312
To learn how to perform individual write operations, see the following guides:
305
313
306
- - :ref:`pymongo-write-insert`
307
- - :ref:`pymongo-write-update`
308
- - :ref:`pymongo-write-replace`
309
- - :ref:`pymongo-write-delete`
314
+ - :ref:`kotlin-sync-write-insert`
315
+ - :ref:`kotlin-sync-write-update`
316
+ - :ref:`kotlin-sync-write-delete`
317
+
318
+ .. - :ref:`kotlin-sync-write-replace`
310
319
311
320
API Documentation
312
321
~~~~~~~~~~~~~~~~~
313
322
314
323
To learn more about any of the methods or types discussed in this
315
324
guide, see the following API Documentation:
316
325
317
- - `bulk_write() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.bulk_write>`__
318
- - `InsertOne <{+api-root+}pymongo/operations.html#pymongo.operations.InsertOne>`__
319
- - `UpdateOne <{+api-root+}pymongo/operations.html#pymongo.operations.UpdateOne>`__
320
- - `UpdateMany <{+api-root+}pymongo/operations.html#pymongo.operations.UpdateMany>`__
321
- - `ReplaceOne <{+api-root+}pymongo/operations.html#pymongo.operations.ReplaceOne>`__
322
- - `DeleteOne <{+api-root+}pymongo/operations.html#pymongo.operations.DeleteOne>`__
323
- - `DeleteMany <{+api-root+}pymongo/operations.html#pymongo.operations.DeleteMany>`__
324
- - `BulkWriteResult <{+api-root+}pymongo/results.html#pymongo.results.BulkWriteResult>`__
325
- - `BulkWriteError <{+api-root+}pymongo/errors.html#pymongo.errors.BulkWriteError>`__
326
+ - `bulkWrite() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/bulk-write.html>`__
327
+ - `DeleteManyModel <{+core-api+}/com/mongodb/client/model/DeleteManyModel.html>`__
328
+ - `DeleteOneModel <{+core-api+}/com/mongodb/client/model/DeleteOneModel.html>`__
329
+ - `InsertOneModel <{+core-api+}/com/mongodb/client/model/InsertOneModel.html>`__
330
+ - `ReplaceOneModel <{+core-api+}/com/mongodb/client/model/ReplaceOneModel.html>`__
331
+ - `UpdateManyModel <{+core-api+}/com/mongodb/client/model/UpdateManyModel.html>`__
332
+ - `UpdateOneModel <{+core-api+}/com/mongodb/client/model/UpdateOneModel.html>`__
0 commit comments