Skip to content

Commit abf5439

Browse files
DOCSP-37537 Delete Operations (#47)
1 parent cfef908 commit abf5439

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

source/includes/write/delete.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# start-delete-one
2+
3+
# end-delete-one
4+
5+
# start-delete-many
6+
7+
# end-delete-many
8+
9+
# start-delete-options
10+
11+
# end-delete-options

source/write-operations.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ Write Data to MongoDB
1414
/write/insert
1515
/write/update
1616
/write/replace
17+
/write/delete
1718

1819
- :ref:`pymongo-write-insert`
1920
- :ref:`pymongo-write-update`
2021
- :ref:`pymongo-write-replace`
21-
22+
- :ref:`pymongo-write-delete`

source/write/delete.txt

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
.. _pymongo-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 {+driver-short+} 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 ``delete_one()`` or
28+
``delete_many()`` methods.
29+
30+
.. .. tip:: Interactive Lab
31+
32+
.. This page includes a short interactive lab that demonstrates how to
33+
.. modify data by using the ``delete_many()`` method. You can complete this
34+
.. lab directly in your browser window without installing MongoDB or a code editor.
35+
36+
.. To start the lab, click the :guilabel:`Open Interactive Tutorial` button at the
37+
.. top of the page. To expand the lab to a full-screen format, click the
38+
.. full-screen button (:guilabel:`⛶`) in the top-right corner of the lab pane.
39+
40+
Sample Data
41+
~~~~~~~~~~~
42+
43+
The examples in this guide use the ``sample_restaurants.restaurants`` collection
44+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
45+
free MongoDB Atlas cluster and load the sample datasets, see the
46+
:ref:`<pymongo-get-started>` tutorial.
47+
48+
Delete Operations
49+
-----------------
50+
51+
You can perform delete operations in MongoDB by using the following methods:
52+
53+
- ``delete_one()``, which deletes *the first document* that matches the search criteria
54+
- ``delete_many()``, which deletes *all documents* that match the search criteria
55+
56+
Each delete method requires a **query filter** document, which specifies the
57+
search criteria that determine which documents to select for removal.
58+
For more information about query filters, see the
59+
:manual:`Query Filter Documents section </core/document/#query-filter-documents>` in
60+
the MongoDB Server manual.
61+
62+
Delete One Document
63+
~~~~~~~~~~~~~~~~~~~
64+
65+
The following example uses the ``delete_one()`` method to remove a document in
66+
the ``restaurants`` collection with a ``name`` value of ``"Ready Penny Inn"``:
67+
68+
.. literalinclude:: /includes/write/delete.py
69+
:start-after: start-delete-one
70+
:end-before: end-delete-one
71+
:language: python
72+
:copyable:
73+
74+
Delete Multiple Documents
75+
~~~~~~~~~~~~~~~~~~~~~~~~~
76+
77+
The following example uses the ``delete_many()`` method to remove all documents
78+
in the ``restaurants`` collection with a ``borough`` value of ``"Brooklyn"``:
79+
80+
.. literalinclude:: /includes/write/delete.py
81+
:start-after: start-delete-many
82+
:end-before: end-delete-many
83+
:language: python
84+
:copyable:
85+
86+
Customize the Delete Operation
87+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88+
89+
The ``delete_one()`` and ``delete_many()`` methods optionally accept additional
90+
parameters, which represent options you can use to configure the delete
91+
operation. If you don't specify any additional options, the driver does not customize
92+
the delete operation.
93+
94+
.. list-table::
95+
:widths: 30 70
96+
:header-rows: 1
97+
98+
* - Property
99+
- Description
100+
101+
* - ``collation``
102+
- | Specifies the kind of language collation to use when sorting
103+
results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>`
104+
in the MongoDB Server manual.
105+
106+
* - ``hint``
107+
- | Gets or sets the index to scan for documents.
108+
For more information, see the :manual:`hint statement </reference/command/delete/#std-label-deletes-array-hint>`
109+
in the MongoDB Server manual.
110+
111+
* - ``session``
112+
- | An instance of ``ClientSession``.
113+
114+
* - ``let``
115+
- | A map of parameter names and values. Values must be constant or closed
116+
expressions that don't reference document fields. For more information,
117+
see the :manual:`let statement
118+
</reference/command/delete/#std-label-delete-let-syntax>` in the
119+
MongoDB Server manual.
120+
121+
* - ``comment``
122+
- | A comment to attach to the operation. For more information, see the :manual:`delete command
123+
fields </reference/command/delete/#command-fields>` guide in the
124+
MongoDB Server manual for more information.
125+
126+
The following code uses the ``delete_many()`` method to delete all documents in
127+
the ``restaurants`` collection with a ``name`` value that includes the string ``"Mongo"``.
128+
It also uses the ``comment`` option to add a comment to the operation:
129+
130+
.. literalinclude:: /includes/write/delete.py
131+
:start-after: start-delete-options
132+
:end-before: end-delete-options
133+
:language: python
134+
:copyable:
135+
136+
.. tip::
137+
138+
If the preceding example used the ``delete_one()`` method instead of
139+
``delete_many()``, the driver would delete only the first document with a
140+
``name`` value that includes ``"Mongo"``.
141+
142+
Return Value
143+
~~~~~~~~~~~~
144+
145+
The ``delete_one()`` and ``delete_many()`` methods return a
146+
``DeleteResult`` type. This type contains the following properties:
147+
148+
- ``deleted_count``, which indicates the number of documents deleted
149+
- ``acknowledged``, which indicates if the server acknowledges the result
150+
- ``raw_result``, which is the raw result returned by the server
151+
152+
.. note::
153+
154+
If the ``acknowledged`` attribute is ``False``, all other attributes of ``DeleteResult``
155+
raise an ``InvalidOperation`` exception when accessed. The driver cannot
156+
determine these values if the server does not acknowledge the write operation.
157+
158+
If the query filter does not match any documents, the driver doesn't delete any
159+
documents and ``deleted_count`` is 0.
160+
161+
API Documentation
162+
~~~~~~~~~~~~~~~~~
163+
164+
To learn more about any of the methods or types discussed in this
165+
guide, see the following API Documentation:
166+
167+
- `delete_one() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.delete_one>`__
168+
- `delete_many() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.delete_many>`__
169+
- `DeleteResult <{+api-root+}pymongo/results.html#pymongo.results.DeleteResult>`__
170+
- `ClientSession <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__
171+
172+
173+
.. .. _pymongo-delete-instruqt-lab:
174+
175+
.. .. instruqt:: /mongodb-docs/tracks/delete-documents---c-net-driver?token=em_69t_l-j0BC_en7Uy
176+
.. :title: delete_many() Lesson
177+
.. :drawer:

0 commit comments

Comments
 (0)