/
db.collection.findOneAndDelete.txt
354 lines (239 loc) · 9.71 KB
/
db.collection.findOneAndDelete.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
================================
db.collection.findOneAndDelete()
================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.findOneAndDelete( filter, options )
.. versionadded:: 3.2
Deletes a single document based on the ``filter`` and
``sort`` criteria, returning the deleted document.
The :method:`~db.collection.findOneAndDelete()` method has the following
form:
.. code-block:: none
db.collection.findOneAndDelete(
<filter>,
{
writeConcern: <document>,
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
collation: <document>
}
)
The :method:`~db.collection.findOneAndDelete()` method takes the following
parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``filter``
- document
- The selection criteria for the deletion. The same :ref:`query
selectors <query-selectors>` as in the :method:`find()
<db.collection.find()>` method are available.
Specify an empty document ``{ }`` to delete the first document returned in
the collection.
If unspecified, defaults to an empty document.
Starting in MongoDB 4.0.12+, the operation
errors if the query argument is not a document.
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern </reference/write-concern>`.
Omit to use the default write concern.
.. code-block:: javascript
{ w: <value>, j: <boolean>, wtimeout: <number> }
See :ref:`Delete A Document Using WriteConcern
<delete-a-document-using-writeconcern>` for usage.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
* - ``projection``
- document
- Optional. A subset of fields to return.
To return all fields in the returned document, omit this parameter.
Starting in MongoDB 4.0.12+, the operation
errors if the projection argument is not a document.
* - ``sort``
- document
- Optional. Specifies a sorting order for the documents matched by the ``filter``.
See :method:`cursor.sort()`.
* - ``maxTimeMS``
- number
- Optional. Specifies a time limit in milliseconds within which the operation must
complete within. Throws an error if the limit is exceeded.
* - ``collation``
- document
- Optional.
.. include:: /includes/extracts/collation-option.rst
:returns:
Returns the deleted document.
Behavior
--------
Document Match
~~~~~~~~~~~~~~
:method:`~db.collection.findOneAndDelete()` deletes the first matching
document in the collection that matches the ``filter``.
The ``sort`` parameter can be used to influence which document is updated.
Projection
~~~~~~~~~~
The ``projection`` parameter takes a document in the following form:
.. code-block:: javascript
{ field1 : < boolean >, field2 : < boolean> ... }
The ``<boolean>`` value can be any of the following:
- ``1`` or ``true`` to include the field. The method returns the
``_id`` field even if it is not explicitly stated in the projection
parameter.
- ``0`` or ``false`` to exclude the field. This can be used on any
field, including ``_id``.
Transactions
~~~~~~~~~~~~
.. include:: /includes/extracts/transactions-supported-operation.rst
.. include:: /includes/extracts/transactions-operations-write-concern.rst
.. include:: /includes/extracts/transactions-usage.rst
.. |operation| replace:: :method:`db.collection.findOneAndDelete()`
.. _findOneAndDelete-examples:
Examples
--------
.. _findOneAndDelete-example-replace-document:
Delete A Document
~~~~~~~~~~~~~~~~~~
The ``scores`` collection contains documents similar to the following:
.. code-block:: javascript
db.scores.insertMany( [
{ _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
{ _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
{ _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
{ _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
{ _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
{ _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
] )
The following operation finds the first document where ``name : M. Tagnum``
and deletes it:
.. code-block:: javascript
db.scores.findOneAndDelete(
{ "name" : "M. Tagnum" }
)
The operation returns the *original* document that has been deleted:
.. code-block:: javascript
{ _id: 6312, name: "M. Tagnum", "assignment" : 5, "points" : 30 }
.. _delete-a-document-using-writeconcern:
Delete A Document Using WriteConcern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``scores`` collection contains documents similar to the following:
.. code-block:: javascript
db.scores.insertMany( [
{ _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
{ _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
{ _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
{ _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
{ _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
{ _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
] )
The following operation uses a write concern document inside of the
:method:`db.collection.findOneAndDelete()` method with options:
- ``w:1`` to requests acknowledgment that the write operation has
propagated to the standalone mongod or the primary in a replica set.
- ``j:true`` to tell the number of MongoDB instances specified in ``w:1``
to have the delete written to on-disk journel.
- ``wtimeout : 1000`` to specify a time limit, in milliseconds,
for the write concern. ``wtimeout`` is only applicable for ``w``
values greater than 1.
.. code-block:: javascript
:emphasize-lines: 3-9
db.scores.findOneAndDelete(
{name: "A. MacDyver"},
{ writeConcern: {
w : 1,
j : true,
wtimeout : 1000
}
}
)
The operation returns the following document:
.. code-block:: javascript
:copyable: false
{ _id: 6305, name: 'A. MacDyver', assignment: 5, points: 24 }
The document is deleted with the writeConcern options specified.
.. _findOneAndDelete-example-sort-and-replace-document:
Sort And Delete A Document
~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``scores`` collection contains documents similar to the following:
.. code-block:: javascript
db.scores.insertMany( [
{ _id: 6305, name : "A. MacDyver", "assignment" : 5, "points" : 24 },
{ _id: 6308, name : "B. Batlock", "assignment" : 3, "points" : 22 },
{ _id: 6312, name : "M. Tagnum", "assignment" : 5, "points" : 30 },
{ _id: 6319, name : "R. Stiles", "assignment" : 2, "points" : 12 },
{ _id: 6322, name : "A. MacDyver", "assignment" : 2, "points" : 14 },
{ _id: 6234, name : "R. Stiles", "assignment" : 1, "points" : 10 }
] )
The following operation first finds all documents where
``name : "A. MacDyver"``. It then sorts by ``points`` ascending before
deleting the document with the lowest points value:
.. code-block:: javascript
db.scores.findOneAndDelete(
{ "name" : "A. MacDyver" },
{ sort : { "points" : 1 } }
)
The operation returns the *original* document that has been deleted:
.. code-block:: javascript
{ _id: 6322, name: "A. MacDyver", "assignment" : 2, "points" : 14 }
.. _findOneAndDelete-example-project-deleted-document:
Projecting the Deleted Document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation uses projection to only return the ``_id`` and
``assignment`` fields in the returned document:
.. code-block:: javascript
db.scores.findOneAndDelete(
{ "name" : "A. MacDyver" },
{ sort : { "points" : 1 }, projection: { "assignment" : 1 } }
)
The operation returns the *original* document with the
``assignment`` and ``_id`` fields:
.. code-block:: javascript
{ _id: 6322, "assignment" : 2 }
.. _findOneAndDelete-example-replace-with-time-limit:
Update Document with Time Limit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation sets a 5ms time limit to complete the deletion:
.. code-block:: javascript
try {
db.scores.findOneAndDelete(
{ "name" : "A. MacDyver" },
{ sort : { "points" : 1 }, maxTimeMS : 5 }
)
}
catch(e){
print(e)
}
If the operation exceeds the time limit, it returns:
.. code-block:: javascript
MongoServerError: operation exceeded time limit: { "ok": 0, "code" : 50, "codeName" : "MaxTimeMSExpired" }
.. note::
This error message has been shortened for brevity.
Specify Collation
~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/collation-versionadded.rst
A collection ``myColl`` has the following documents:
.. code-block:: javascript
db.myColl.insertMany( [
{ _id: 1, category: "café", status: "A" },
{ _id: 2, category: "cafe", status: "a" },
{ _id: 3, category: "cafE", status: "a" }
] )
The following operation includes the :ref:`collation <collation>`
option:
.. code-block:: javascript
db.myColl.findOneAndDelete(
{ category: "cafe", status: "a" },
{ collation: { locale: "fr", strength: 1 } }
);
The operation returns the following document:
.. code-block:: javascript
{ "_id" : 1, "category" : "café", "status" : "A" }