-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMongoDBCollection-deleteMany.txt
136 lines (92 loc) · 3.39 KB
/
MongoDBCollection-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
=================================
MongoDB\\Collection::deleteMany()
=================================
.. meta::
:description: Delete multiple documents in a collection with the MongoDB PHP Library, with options for collation, hint, and write concern.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Collection::deleteMany()
Deletes all documents that match the filter criteria.
.. code-block:: php
function deleteMany(
array|object $filter,
array $options = []
): MongoDB\DeleteResult
Parameters
----------
``$filter`` : array|object
The filter criteria that specifies the documents to delete.
``$options`` : array
An array specifying the desired options.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - collation
- array|object
- .. include:: /includes/extracts/collection-option-collation.rst
* - comment
- mixed
- .. include:: /includes/extracts/common-option-comment.rst
.. include:: /includes/extracts/option-requires-4.4.rst
.. versionadded:: 1.13
* - hint
- string|array|object
- .. include:: /includes/extracts/common-option-hint.rst
.. include:: /includes/extracts/option-requires-4.4.rst
.. versionadded:: 1.7
* - let
- array|object
- .. include:: /includes/extracts/common-option-let.rst
.. versionadded:: 1.13
* - session
- :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
- .. include:: /includes/extracts/common-option-session.rst
.. versionadded:: 1.3
* - writeConcern
- :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
- .. include:: /includes/extracts/collection-option-writeConcern.rst
.. include:: /includes/extracts/common-option-writeConcern-transaction.rst
Return Values
-------------
A :phpclass:`MongoDB\DeleteResult` object, which encapsulates a
:php:`MongoDB\Driver\WriteResult <class.mongodb-driver-writeresult>` object.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-bulkwriteexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Behavior
--------
.. include:: /includes/extracts/note-bson-comparison.rst
.. include:: /includes/extracts/bulkwriteexception-result.rst
Example
-------
The following example deletes all of the documents in the ``users`` collection
that have ``"ny"`` as the value for the ``state`` field:
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->test->users;
$collection->drop();
$collection->insertOne(['name' => 'Bob', 'state' => 'ny']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$deleteResult = $collection->deleteMany(['state' => 'ny']);
printf("Deleted %d document(s)\n", $deleteResult->getDeletedCount());
The output would then resemble:
.. code-block:: none
Deleted 2 document(s)
See Also
--------
- :phpmethod:`MongoDB\Collection::deleteOne()`
- :phpmethod:`MongoDB\Collection::bulkWrite()`
- :ref:`php-write-delete`
- :manual:`delete </reference/command/delete>` command reference in the MongoDB
manual