-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.collection.remove.txt
338 lines (235 loc) · 8.77 KB
/
db.collection.remove.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
======================
db.collection.remove()
======================
.. default-domain:: mongodb
.. meta::
:keywords: deprecated
:description: The remove method is deprecated should be replaced by deleteOne or deleteMany.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. include:: /includes/fact-mongosh-shell-method-deprecated.rst
Definition
----------
.. method:: db.collection.remove()
Removes documents from a collection.
:returns: A :ref:`writeresults-remove` object that contains the
status of the operation.
Syntax
------
The :method:`db.collection.remove()` method can have one of two
syntaxes. The :method:`~db.collection.remove()` method can take a
query document and an optional ``justOne`` boolean:
.. code-block:: javascript
db.collection.remove(
<query>,
<justOne>
)
Or the method can take a query document and an optional remove
options document:
.. versionchanged:: 5.0
.. code-block:: javascript
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>,
let: <document> // Added in MongoDB 5.0
}
)
The :method:`~db.collection.remove()` method takes the following
parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``query``
- document
- Specifies deletion criteria using :ref:`query operators
<query-selectors>`. To delete all documents in a collection,
pass an empty document (``{}``).
* - ``justOne``
- boolean
- Optional. To limit the deletion to just one document, set to ``true``. Omit to
use the default value of ``false`` and delete all documents matching
the deletion criteria.
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern
</reference/write-concern>`. Omit to use the default write concern.
See :ref:`remove-wc`.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
* - ``collation``
- document
- Optional.
.. include:: /includes/extracts/collation-option.rst
* - :ref:`let <db.collection.remove-let-syntax>`
- document
- .. _db.collection.remove-let-syntax:
Optional.
.. include:: /includes/let-variables-syntax.rst
.. include:: /includes/let-variables-syntax-note.rst
For a complete example using ``let`` and variables,
see :ref:`db.collection.remove-let-example`.
.. versionadded:: 5.0
Behavior
--------
.. _remove-wc:
Write Concern
~~~~~~~~~~~~~
The :method:`~db.collection.remove()` method uses the
:dbcommand:`delete` command, which uses the default :doc:`write concern
</reference/write-concern>`. To specify a different write concern,
include the write concern in the options parameter.
Query Considerations
~~~~~~~~~~~~~~~~~~~~
By default, :method:`~db.collection.remove()` removes all documents
that match the ``query`` expression. Specify the ``justOne`` option to
limit the operation to removing a single document. To delete a single
document sorted by a specified order, use the :ref:`findAndModify()
<findAndModify-wrapper-sorted-remove>` method.
When removing multiple documents, the remove operation may interleave
with other read and/or write operations to the collection.
Time Series Collections
~~~~~~~~~~~~~~~~~~~~~~~
You cannot use the :method:`~db.collection.remove()` method on a
:term:`time series collection`.
Sharded Collections
~~~~~~~~~~~~~~~~~~~
.. |single-modification-operation-names| replace:: :method:`~db.collection.remove()`
.. |single-modification-operation-option| replace:: ``justOne: true``
.. include:: /includes/fact-single-modification-in-sharded-collections.rst
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.remove()`
Examples
--------
The following are examples of the :method:`~db.collection.remove()` method.
Remove All Documents from a Collection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To remove all documents in a collection, call the :method:`remove
<db.collection.remove()>` method with an empty query document ``{}``.
The following operation deletes all documents from the :doc:`bios
collection </reference/bios-example-collection>`:
.. code-block:: javascript
db.bios.remove( { } )
This operation is not equivalent to the
:method:`~db.collection.drop()` method.
To remove all documents from a collection, it may be more efficient
to use the :method:`~db.collection.drop()` method to drop the entire
collection, including the indexes, and then recreate the collection
and rebuild the indexes.
Remove All Documents that Match a Condition
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To remove the documents that match a deletion criteria, call the
:method:`~db.collection.remove()` method with the ``<query>``
parameter:
The following operation removes all the documents from the collection
``products`` where ``qty`` is greater than ``20``:
.. code-block:: javascript
db.products.remove( { qty: { $gt: 20 } } )
Override Default Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation to a replica set removes all the documents from
the collection ``products`` where ``qty`` is greater than ``20`` and
specifies a :ref:`write concern <write-concern>` of ``w: 2``
with a ``wtimeout`` of 5000 milliseconds. This operation either returns
after the write propagates to both the primary and one secondary, or
times out after 5 seconds.
.. code-block:: javascript
db.products.remove(
{ qty: { $gt: 20 } },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)
Remove a Single Document that Matches a Condition
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To remove the first document that match a deletion criteria, call the
:method:`remove <db.collection.remove()>` method with the ``query``
criteria and the ``justOne`` parameter set to ``true`` or ``1``.
The following operation removes the first document from the collection
``products`` where ``qty`` is greater than ``20``:
.. code-block:: javascript
db.products.remove( { qty: { $gt: 20 } }, true )
Specify Collation
~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/collation-versionadded.rst
A collection ``myColl`` has the following documents:
.. code-block:: javascript
{ _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.remove(
{ category: "cafe", status: "A" },
{ collation: { locale: "fr", strength: 1 } }
)
.. _db.collection.remove-let-example:
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~
.. |let-option| replace:: :ref:`let <db.collection.remove-let-syntax>`
.. include:: /includes/let-example-introduction.rst
.. include:: /includes/let-example-delete-flavors.rst
.. code-block:: javascript
db.cakeFlavors.remove(
{ $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
{ let : { targetFlavor: "strawberry" } }
)
.. _writeresults-remove:
WriteResult
-----------
Successful Results
~~~~~~~~~~~~~~~~~~
The :method:`~db.collection.remove()` returns a :method:`WriteResult`
object that contains the status of the operation. Upon success, the
:method:`WriteResult` object contains information on the number of
documents removed:
.. code-block:: javascript
WriteResult({ "nRemoved" : 4 })
.. seealso::
:data:`WriteResult.nRemoved`
Write Concern Errors
~~~~~~~~~~~~~~~~~~~~
If the :method:`~db.collection.remove()` method encounters write
concern errors, the results include the
:data:`WriteResult.writeConcernError` field:
.. code-block:: javascript
WriteResult({
"nRemoved" : 7,
"writeConcernError" : {
"code" : 64,
"codeName" : "WriteConcernTimeout",
"errmsg" : "waiting for replication timed out",
"errInfo" : {
"wtimeout" : true,
"writeConcern" : {
"w" : "majority",
"wtimeout" : 1,
"provenance" : "getLastErrorDefaults"
}
}
}
})
.. seealso::
- :data:`WriteResult.writeConcernError`
Errors Unrelated to Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the :method:`~db.collection.remove()` method encounters a non-write
concern error, the results include :data:`WriteResult.writeError` field:
.. code-block:: javascript
WriteResult({
"nRemoved" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "unknown top level operator: $invalidFieldName"
}
})