1
- =================================
1
+ ================================
2
2
db.collection.findOneAndDelete()
3
- =================================
3
+ ================================
4
4
5
5
.. default-domain:: mongodb
6
6
@@ -27,11 +27,12 @@ Definition
27
27
The :method:`~db.collection.findOneAndDelete()` method has the following
28
28
form:
29
29
30
- .. code-block:: javascript
30
+ .. code-block:: none
31
31
32
32
db.collection.findOneAndDelete(
33
33
<filter>,
34
34
{
35
+ writeConcern: <document>,
35
36
projection: <document>,
36
37
sort: <document>,
37
38
maxTimeMS: <number>,
@@ -48,15 +49,11 @@ Definition
48
49
:widths: 20 20 80
49
50
50
51
* - Parameter
51
-
52
52
- Type
53
-
54
53
- Description
55
-
54
+
56
55
* - ``filter``
57
-
58
56
- document
59
-
60
57
- The selection criteria for the deletion. The same :ref:`query
61
58
selectors <query-selectors>` as in the :method:`find()
62
59
<db.collection.find()>` method are available.
@@ -66,62 +63,56 @@ Definition
66
63
67
64
If unspecified, defaults to an empty document.
68
65
69
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+ ), the operation
66
+ Starting in MongoDB 4.2 (and 4.0.12+), the operation
70
67
errors if the query argument is not a document.
68
+
69
+ * - ``writeConcern``
70
+ - document
71
+ - Optional. A document expressing the :doc:`write concern </reference/write-concern>`.
72
+ Omit to use the default write concern.
73
+
74
+ .. code-block:: javascript
75
+
76
+ { w: <value>, j: <boolean>, wtimeout: <number> }
71
77
78
+ See :ref:`Delete A Document Using WriteConcern
79
+ <delete-a-document-using-writeconcern>` for usage.
80
+
81
+ .. include:: /includes/extracts/transactions-operations-write-concern.rst
72
82
73
-
74
83
* - ``projection``
75
-
76
84
- document
77
-
78
85
- Optional. A subset of fields to return.
79
86
80
87
To return all fields in the returned document, omit this parameter.
81
88
82
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+ ), the operation
89
+ Starting in MongoDB 4.2 (and 4.0.12+), the operation
83
90
errors if the projection argument is not a document.
84
-
85
-
86
-
91
+
87
92
* - ``sort``
88
-
89
93
- document
90
-
91
94
- Optional. Specifies a sorting order for the documents matched by the ``filter``.
92
95
93
- Starting in MongoDB 4.2 (and 4.0.12+, 3.6.14+, and 3.4.23+), the operation
94
- errors if the sort argument is not a document.
96
+ Starting in MongoDB 4.2 (and 4.0.12+) the operation errors if the sort
97
+ argument is not a document.
95
98
96
99
See :method:`cursor.sort()`.
97
-
98
-
99
-
100
+
100
101
* - ``maxTimeMS``
101
-
102
102
- number
103
-
104
103
- Optional. Specifies a time limit in milliseconds within which the operation must
105
104
complete within. Throws an error if the limit is exceeded.
106
-
107
-
108
-
105
+
109
106
* - ``collation``
110
-
111
107
- document
112
-
113
108
- Optional.
114
-
115
- .. include:: /includes/extracts/collation-option.rst
116
-
117
-
118
-
119
109
110
+ .. include:: /includes/extracts/collation-option.rst
120
111
121
112
:returns:
122
113
123
114
Returns the deleted document.
124
-
115
+
125
116
Behavior
126
117
--------
127
118
@@ -189,7 +180,7 @@ Transactions
189
180
.. _findOneAndDelete-examples:
190
181
191
182
Examples
192
- ~~~~~~~~
183
+ --------
193
184
194
185
.. _findOneAndDelete-example-replace-document:
195
186
@@ -200,12 +191,14 @@ The ``scores`` collection contains documents similar to the following:
200
191
201
192
.. code-block:: javascript
202
193
203
- { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
204
- { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
205
- { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
206
- { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
207
- { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
208
- { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
194
+ db.scores.insertMany( [
195
+ { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
196
+ { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
197
+ { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
198
+ { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
199
+ { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
200
+ { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
201
+ ] )
209
202
210
203
The following operation finds the first document where ``name : M. Tagnum``
211
204
and deletes it:
@@ -222,6 +215,58 @@ The operation returns the *original* document that has been deleted:
222
215
223
216
{ _id: 6312, name: "M. Tagnum", "assignment" : 5, "points" : 30 }
224
217
218
+ .. _delete-a-document-using-writeconcern:
219
+
220
+ Delete A Document Using WriteConcern
221
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222
+
223
+ The ``scores`` collection contains documents similar to the following:
224
+
225
+ .. code-block:: javascript
226
+
227
+ db.scores.insertMany( [
228
+ { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
229
+ { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
230
+ { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
231
+ { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
232
+ { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
233
+ { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
234
+ ] )
235
+
236
+ The following operation uses a write concern document inside of the
237
+ :method:`db.collection.findOneAndDelete()` method with options:
238
+
239
+ - ``w:1`` to requests acknowledgment that the write operation has
240
+ propagated to the standalone mongod or the primary in a replica set.
241
+ - ``j:true`` to tell the number of MongoDB instances specified in ``w:1``
242
+ to have the delete written to on-disk journel.
243
+ - ``wtimeout : 1000`` to specify a time limit, in milliseconds,
244
+ for the write concern. ``wtimeout`` is only applicable for ``w``
245
+ values greater than 1.
246
+
247
+
248
+ .. code-block:: javascript
249
+ :emphasize-lines: 3-9
250
+
251
+ db.scores.findOneAndDelete(
252
+ {name: "A. MacDyver"},
253
+ { writeConcern: {
254
+ w : 1,
255
+ j : true,
256
+ wtimeout : 1000
257
+ }
258
+ }
259
+ )
260
+
261
+ The operation returns the following document:
262
+
263
+ .. code-block:: javascript
264
+ :copyable: false
265
+
266
+ { _id: 6305, name: 'A. MacDyver', assignment: 5, points: 24 }
267
+
268
+ The document is deleted with the writeConcern options specified.
269
+
225
270
.. _findOneAndDelete-example-sort-and-replace-document:
226
271
227
272
Sort And Delete A Document
@@ -231,12 +276,14 @@ The ``scores`` collection contains documents similar to the following:
231
276
232
277
.. code-block:: javascript
233
278
234
- { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
235
- { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
236
- { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
237
- { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
238
- { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
239
- { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
279
+ db.scores.insertMany( [
280
+ { _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
281
+ { _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
282
+ { _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
283
+ { _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
284
+ { _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
285
+ { _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
286
+ ] )
240
287
241
288
The following operation first finds all documents where
242
289
``name : "A. MacDyver"``. It then sorts by ``points`` ascending before
@@ -289,18 +336,22 @@ The following operation sets a 5ms time limit to complete the deletion:
289
336
try {
290
337
db.scores.findOneAndDelete(
291
338
{ "name" : "A. MacDyver" },
292
- { sort : { "points" : 1 }, maxTimeMS : 5 };
293
- );
339
+ { sort : { "points" : 1 }, maxTimeMS : 5 }
340
+ )
294
341
}
295
342
catch(e){
296
- print(e);
343
+ print(e)
297
344
}
298
345
299
346
If the operation exceeds the time limit, it returns:
300
347
301
348
.. code-block:: javascript
349
+
350
+ MongoServerError: operation exceeded time limit: { "ok": 0, "code" : 50, "codeName" : "MaxTimeMSExpired" }
351
+
352
+ .. note::
302
353
303
- Error: findAndModifyFailed failed: { "ok" : 0, "errmsg" : "operation exceeded time limit", "code" : 50 }
354
+ This error message has been shortened for brevity.
304
355
305
356
Specify Collation
306
357
~~~~~~~~~~~~~~~~~
@@ -311,9 +362,11 @@ A collection ``myColl`` has the following documents:
311
362
312
363
.. code-block:: javascript
313
364
314
- { _id: 1, category: "café", status: "A" }
315
- { _id: 2, category: "cafe", status: "a" }
316
- { _id: 3, category: "cafE", status: "a" }
365
+ db.myColl.insertMany( [
366
+ { _id: 1, category: "café", status: "A" },
367
+ { _id: 2, category: "cafe", status: "a" },
368
+ { _id: 3, category: "cafE", status: "a" }
369
+ ] )
317
370
318
371
The following operation includes the :ref:`collation <collation>`
319
372
option:
0 commit comments