@@ -16,9 +16,8 @@ Retryable Writes
16
16
17
17
Retryable writes allow MongoDB drivers to automatically retry certain
18
18
write operations a single time if they encounter network errors, or if
19
- they cannot find a healthy :term:`primary` in the
20
- :ref:`replica sets <replication>` or :ref:`sharded cluster
21
- <sharding-introduction>`. [#duplicate-key-update]_
19
+ they cannot find a healthy :term:`primary` in the :ref:`replica set
20
+ <replication>` or :ref:`sharded cluster <sharding-introduction>`.
22
21
23
22
Prerequisites
24
23
-------------
@@ -105,20 +104,15 @@ cannot be :writeconcern:`{w: 0} <\<number\>>`.
105
104
* - | :method:`db.collection.insertOne()`
106
105
| :method:`db.collection.insert()`
107
106
| :method:`db.collection.insertMany()`
108
-
109
- - Insert operations.
107
+ - Insert operations
110
108
111
109
* - | :method:`db.collection.updateOne()`
112
110
| :method:`db.collection.replaceOne()`
113
- | :method:`db.collection.save()`
114
- | :method:`db.collection.update()` where ``multi`` is ``false``
115
-
116
- - Single-document update operations. [#duplicate-key-update]_
111
+ - Single-document update operations
117
112
118
113
* - | :method:`db.collection.deleteOne()`
119
114
| :method:`db.collection.remove()` where ``justOne`` is ``true``
120
-
121
- - Single document delete operations.
115
+ - Single document delete operations
122
116
123
117
* - | :method:`db.collection.findAndModify()`
124
118
| :method:`db.collection.findOneAndDelete()`
@@ -165,16 +159,6 @@ cannot be :writeconcern:`{w: 0} <\<number\>>`.
165
159
retryable write or in a :doc:`transaction </core/transactions>`. For
166
160
details, see :ref:`update-shard-key`.
167
161
168
- .. [#duplicate-key-update]
169
-
170
- MongoDB 4.2 will retry certain single-document upserts
171
- (update with ``upsert: true`` and ``multi: false``) that encounter a
172
- duplicate key exception. See :ref:`retryable-update-upsert` for
173
- conditions.
174
-
175
- Prior to MongoDB 4.2, MongoDB would not retry upsert operations
176
- that encountered a duplicate key error.
177
-
178
162
Behavior
179
163
--------
180
164
@@ -203,163 +187,6 @@ the failover period exceeds :urioption:`serverSelectionTimeoutMS`.
203
187
applications starts responding (without a restart), the write
204
188
operation may be retried and applied again.
205
189
206
- .. _retryable-update-upsert:
207
-
208
- Duplicate Key Errors on Upsert
209
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210
-
211
- MongoDB 4.2 will retry single-document upsert operations
212
- (i.e ``upsert : true`` and ``multi : false``) that
213
- fail due to a duplicate key error *only if* the operation meets
214
- *all* of the following conditions:
215
-
216
- - The target collection has a unique index that caused the duplicate key
217
- error.
218
-
219
- - The update match condition is either:
220
-
221
- - A single equality predicate
222
-
223
- ``{ "fieldA" : "valueA" }``,
224
-
225
- *or*
226
-
227
- - a logical AND of equality predicates
228
-
229
- ``{ "fieldA" : "valueA", "fieldB" : "valueB" }``
230
-
231
- - The set of fields in the unique index key pattern matches the set
232
- of fields in the update query predicate.
233
-
234
- - The update operation does not modify any of the fields in the
235
- query predicate.
236
-
237
- The following table contains examples of upsert operations that
238
- the server can or cannot retry on a duplicate key error:
239
-
240
- .. list-table::
241
- :header-rows: 1
242
- :widths: 30 40 30
243
-
244
- * - Unique Index Key Pattern
245
- - Update Operation
246
- - Retryable
247
-
248
- * - .. code-block:: javascript
249
- :copyable: false
250
-
251
- { _id : 1 }
252
- - .. code-block:: javascript
253
- :copyable: false
254
-
255
- db.collName.updateOne(
256
- { _id : ObjectId("1aa1c1efb123f14aaa167aaa") },
257
- { $set : { fieldA : 25 } },
258
- { upsert : true }
259
- )
260
- - Yes
261
-
262
- * - .. code-block:: javascript
263
- :copyable: false
264
-
265
- { fieldA : 1 }
266
- - .. code-block:: javascript
267
- :copyable: false
268
-
269
- db.collName.updateOne(
270
- { fieldA : { $in : [ 25 ] } },
271
- { $set : { fieldB : "someValue" } },
272
- { upsert : true }
273
- )
274
- - Yes
275
-
276
- * - .. code-block:: javascript
277
- :copyable: false
278
-
279
- {
280
- fieldA : 1,
281
- fieldB : 1
282
- }
283
- - .. code-block:: javascript
284
- :copyable: false
285
-
286
- db.collName.updateOne(
287
- { fieldA : 25, fieldB : "someValue" },
288
- { $set : { fieldC : false } },
289
- { upsert : true }
290
- )
291
- - Yes
292
-
293
- * - .. code-block:: javascript
294
- :copyable: false
295
-
296
- { fieldA : 1 }
297
- - .. code-block:: javascript
298
- :copyable: false
299
-
300
- db.collName.updateOne(
301
- { fieldA : { $lte : 25 } },
302
- { $set : { fieldC : true } },
303
- { upsert : true }
304
- )
305
- - No
306
-
307
- The query predicate on ``fieldA`` is not an equality
308
-
309
- * - .. code-block:: javascript
310
- :copyable: false
311
-
312
- { fieldA : 1 }
313
- - .. code-block:: javascript
314
- :copyable: false
315
-
316
- db.collName.updateOne(
317
- { fieldA : { $in : [ 25 ] } },
318
- { $set : { fieldA : 20 } },
319
- { upsert : true }
320
- )
321
- - No
322
-
323
- The update operation modifies fields specified in the
324
- query predicate.
325
-
326
- * - .. code-block:: javascript
327
- :copyable: false
328
-
329
- { _id : 1 }
330
- - .. code-block:: javascript
331
- :copyable: false
332
-
333
- db.collName.updateOne(
334
- { fieldA : { $in : [ 25 ] } },
335
- { $set : { fieldA : 20 } },
336
- { upsert : true }
337
- )
338
- - No
339
-
340
- The set of query predicate fields (``fieldA``) does not
341
- match the set of index key fields (``_id``).
342
-
343
- * - .. code-block:: javascript
344
- :copyable: false
345
-
346
- { fieldA : 1 }
347
- - .. code-block:: javascript
348
- :copyable: false
349
-
350
- db.collName.updateOne(
351
- { fieldA : 25, fieldC : true },
352
- { $set : { fieldD : false } },
353
- { upsert : true }
354
- )
355
- - No
356
-
357
- The set of query predicate fields (``fieldA``, ``fieldC``)
358
- does not match the set of index key fields (``fieldA``).
359
-
360
- Prior to MongoDB 4.2, MongoDB retryable writes did not support
361
- retrying upserts which failed due to duplicate key errors.
362
-
363
190
Diagnostics
364
191
~~~~~~~~~~~
365
192
0 commit comments