-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.collection.deleteMany.txt
180 lines (117 loc) · 3.86 KB
/
db.collection.deleteMany.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
==========================
db.collection.deleteMany()
==========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.deleteMany()
Removes all documents that match the ``filter`` from a collection.
.. code-block:: javascript
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>
}
)
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``filter``
- document
- Specifies deletion criteria using :doc:`query operators
</reference/operator>`.
To delete all documents in a collection, pass in an empty document (``{ }``).
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern
</reference/write-concern>`. Omit to use the default write concern.
:returns:
A document containing:
- A boolean ``acknowledged`` as ``true`` if the operation ran with
:term:`write concern` or ``false`` if write concern was disabled
- ``deletedCount`` containing the number of deleted documents
Behavior
--------
.. _deleteMany-capped-collection:
Capped Collections
~~~~~~~~~~~~~~~~~~
:method:`~db.collection.deleteMany()` throws a ``WriteError`` exception
if used on a :term:`capped collection`. To remove all documents from a capped
collection, use :method:`db.collection.drop()` instead.
.. _deleteMany-delete-multiple-documents:
Delete a Single Document
~~~~~~~~~~~~~~~~~~~~~~~~
To delete a single document, use :method:`db.collection.deleteOne()` instead.
Alternatively, use a field that is a part of a :term:`unique index` such as
``_id``.
Examples
--------
.. _deleteMany-example-delete-multiple-document:
Delete Multiple Documents
~~~~~~~~~~~~~~~~~~~~~~~~~
The ``orders`` collection has documents with the following structure:
.. code-block:: javascript
{
_id: ObjectId("563237a41a4d68582c2509da"),
stock: "Brent Crude Futures",
qty: 250,
type: "buy-limit",
limit: 48.90,
creationts: ISODate("2015-11-01T12:30:15Z"),
expiryts: ISODate("2015-11-01T12:35:15Z"),
client: "Crude Traders Inc."
}
The following operation deletes all documents where ``client : "Crude Traders
Inc."``:
.. code-block:: javascript
try {
db.orders.deleteMany( { "client" : "Crude Traders Inc." } );
} catch (e) {
print (e);
}
The operation returns:
.. code-block:: javascript
{ "acknowledged" : true, "deletedCount" : 10 }
The following operation deletes all documents where ``stock : "Brent Crude
Futures"`` and ``limit`` is greater than ``48.88``:
.. code-block:: javascript
try {
db.orders.deleteMany( { "stock" : "Brent Crude Futures", "limit" : { $gt : 48.88 } } );
} catch (e) {
print (e);
}
The operation returns:
.. code-block:: javascript
{ "acknowledged" : true, "deletedCount" : 8 }
.. _deleteMany-example-update-with-write-concern:
deleteMany() with Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Given a three member replica set, the following operation specifies a
``w`` of ``majority`` and ``wtimeout`` of ``100``:
.. code-block:: javascript
try {
db.orders.deleteMany(
{ "client" : "Crude Traders Inc." },
{ w : "majority", wtimeout : 100 }
);
} catch (e) {
print (e);
}
If the acknowledgement takes longer than the ``wtimeout`` limit, the following
exception is thrown:
.. code-block:: javascript
WriteConcernError({
"code" : 64,
"errInfo" : {
"wtimeout" : true
},
"errmsg" : "waiting for replication timed out"
})