Skip to content

Commit 4470125

Browse files
committed
DOCSP-41970: Delete documents
1 parent cd4c81f commit 4470125

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

source/includes/write/delete.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Ensure you have MongoDB PHP library installed
3+
4+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
5+
$client = new MongoDB\Client($uri);
6+
7+
// start-db-coll
8+
$collection = $client->sample_restaurants->restaurants;
9+
// end-db-coll
10+
11+
// Deletes a document that has a "name" value of "Ready Penny Inn"
12+
// start-delete-one
13+
$result = $collection->deleteOne(['name' => 'Ready Penny Inn']);
14+
// end-delete-one
15+
16+
// Deletes documents that have a "borough" value of "Brooklyn"
17+
// start-delete-many
18+
$result = $collection->deleteMany(['borough' => 'Brooklyn']);
19+
// end-delete-many
20+
21+
// Deletes matching documents and attaches a comment to the operation
22+
// start-delete-options
23+
$result = $collection->deleteMany(
24+
['name' => new MongoDB\BSON\Regex('Mongo', '')],
25+
['comment' => 'Deleting Mongo restaurants'],
26+
);
27+
// end-delete-options
28+
29+
// Deletes and prints the number of documents that have a "cuisine" value of "Greek"
30+
// start-delete-count
31+
$result = $collection->deleteMany(['cuisine' => 'Greek']);
32+
echo $result->getDeletedCount() . PHP_EOL;
33+
// end-delete-count
34+
35+
?>

source/write.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. _php-write:
2+
3+
=====================
4+
Write Data to MongoDB
5+
=====================
6+
7+
.. toctree::
8+
:titlesonly:
9+
:maxdepth: 1
10+
11+
/write/delete

source/write/delete.txt

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
.. _php-write-delete:
2+
3+
================
4+
Delete Documents
5+
================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: remove, drop, code example
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use the {+php-library+} to remove
24+
documents from a MongoDB collection by performing **delete operations**.
25+
26+
A delete operation removes one or more documents from a MongoDB collection.
27+
You can perform a delete operation by using the ``MongoDB\Collection::deleteOne()``
28+
or ``MongoDB\Collection::deleteMany()`` methods.
29+
30+
Sample Data
31+
~~~~~~~~~~~
32+
33+
The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants``
34+
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection
35+
from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster
36+
and assign the following value to your ``$collection`` variable:
37+
38+
.. literalinclude:: /includes/read/delete.php
39+
:language: php
40+
:dedent:
41+
:start-after: start-db-coll
42+
:end-before: end-db-coll
43+
44+
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the
45+
:atlas:`Get Started with Atlas </getting-started>` guide.
46+
47+
Delete Operations
48+
-----------------
49+
50+
You can perform delete operations by using the following methods:
51+
52+
- ``MongoDB\Collection::deleteOne()``, which deletes *the first document*
53+
that matches the search criteria
54+
- ``MongoDB\Collection::deleteMany()``, which deletes *all documents* that
55+
match the search criteria
56+
57+
Each delete method requires a **query filter** document, which specifies the
58+
search criteria to determine which documents to select for removal.
59+
For more information about query filters, see the
60+
:manual:`Query Filter Documents section </core/document/#query-filter-documents>` in
61+
the {+mdb-server+} manual.
62+
63+
Delete One Document
64+
~~~~~~~~~~~~~~~~~~~
65+
66+
The following example uses the ``deleteOne()`` method to remove a document in
67+
the ``restaurants`` collection that has a ``name`` value of ``'Ready Penny Inn'``:
68+
69+
.. literalinclude:: /includes/write/delete.php
70+
:start-after: start-delete-one
71+
:end-before: end-delete-one
72+
:language: php
73+
:dedent:
74+
75+
Delete Multiple Documents
76+
~~~~~~~~~~~~~~~~~~~~~~~~~
77+
78+
The following example uses the ``deleteMany()`` method to remove all documents
79+
in the ``restaurants`` collection that have a ``borough`` value of ``'Brooklyn'``:
80+
81+
.. literalinclude:: /includes/write/delete.php
82+
:start-after: start-delete-many
83+
:end-before: end-delete-many
84+
:language: php
85+
:dedent:
86+
87+
Modify the Delete Operation
88+
---------------------------
89+
90+
You can modify the behavior of the ``MongoDB\Collection::deleteOne()`` and
91+
``MongoDB\Collection::deleteMany()`` methods by by passing an array that
92+
specifies option values as a parameter. The following table describes the
93+
options you can set in the array:
94+
95+
.. list-table::
96+
:widths: 30 70
97+
:header-rows: 1
98+
99+
* - Option
100+
- Description
101+
102+
* - ``collation``
103+
- | Specifies the kind of language collation to use when sorting
104+
results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>`
105+
in the {+mdb-server+} manual.
106+
107+
* - ``writeConcern``
108+
- | Sets the write concern for the operation.
109+
For more information, see :manual:`Write Concern </reference/write-concern/>`
110+
in the {+mdb-server+} manual.
111+
112+
* - ``hint``
113+
- | Gets or sets the index to scan for documents.
114+
For more information, see the :manual:`hint statement </reference/command/delete/#std-label-deletes-array-hint>`
115+
in the {+mdb-server+} manual.
116+
117+
* - ``let``
118+
- | Specifies a document with a list of values to improve operation readability.
119+
Values must be constant or closed expressions that don't reference document
120+
fields. For more information, see the :manual:`let statement
121+
</reference/command/delete/#std-label-delete-let-syntax>` in the
122+
{+mdb-server+} manual.
123+
124+
* - ``session``
125+
- | Specifies the client session to associate with the operation. For more
126+
information, see :manual:`Session </reference/method/Session/>` in the
127+
{+mdb-server+} manual.
128+
129+
* - ``comment``
130+
- | Attaches a comment to the operation. For more information, see the :manual:`delete command
131+
fields </reference/command/delete/#command-fields>` guide in the
132+
{+mdb-server+} manual.
133+
134+
Example
135+
~~~~~~~
136+
137+
The following example calls the ``deleteMany()`` method to delete all documents in
138+
the ``restaurants`` collection that have a ``name`` value containing the string ``'Mongo'``.
139+
It also sets the ``comment`` option in an array parameter to add a comment to the
140+
operation:
141+
142+
.. literalinclude:: /includes/write/delete.php
143+
:start-after: start-delete-options
144+
:end-before: end-delete-options
145+
:language: php
146+
:dedent:
147+
148+
.. tip::
149+
150+
If the preceding example used the ``deleteOne()`` method instead of
151+
``deleteMany()``, the driver would delete only the first document that has
152+
a ``name`` value containing ``'Mongo'``.
153+
154+
Return Value
155+
------------
156+
157+
The ``MongoDB\Collection::deleteOne()`` and ``MongoDB\Collection::deleteMany()``
158+
methods return a ``MongoDB\DeleteResult`` object. This class contains the
159+
following member functions:
160+
161+
- ``isAcknowledged()``, which returns a boolean indicating whether the operation
162+
was acknowledged
163+
- ``getDeletedCount()``, which returns the number of documents deleted
164+
165+
If the query filter does not match any documents, the driver doesn't delete any
166+
documents and ``getDeletedCount()`` returns ``0``.
167+
168+
Example
169+
~~~~~~~
170+
171+
The following example calls the ``deleteMany()`` method to delete documents
172+
that have a ``cuisine`` value of ``'Greek'``. It then calls the ``getDeletedCount()``
173+
member function to print the number of deleted documents:
174+
175+
.. io-code-block::
176+
177+
.. input:: /includes/write/delete.php
178+
:start-after: start-delete-count
179+
:end-before: end-delete-count
180+
:language: php
181+
:dedent:
182+
183+
.. output::
184+
185+
Deleted documents: 111
186+
187+
API Documentation
188+
~~~~~~~~~~~~~~~~~
189+
190+
To learn more about any of the methods or types discussed in this
191+
guide, see the following API documentation:
192+
193+
- `MongoDB\\Collection::deleteOne() <{+api+}/method/MongoDBCollection-deleteOne/>`__
194+
- `MongoDB\\Collection::deleteMany() <{+api+}/method/MongoDBCollection-deleteMany/>`__
195+
- `MongoDB\\DeleteResult <{+api+}/class/MongoDBDeleteResult/>`__

0 commit comments

Comments
 (0)