Skip to content

Commit c454cfc

Browse files
authored
DOCSP-44859: Add delete_one() example (#156)
1 parent 6700bfd commit c454cfc

File tree

2 files changed

+48
-7
lines changed
  • source

2 files changed

+48
-7
lines changed

source/fundamentals/crud/write-operations/delete.txt

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ This guide includes the following sections:
3131
- :ref:`Delete Operations <rust-delete-operations>` describes how to use the
3232
driver to execute delete operations
3333

34+
- :ref:`Delete Examples <rust-delete-operations>` provides code examples
35+
for the delete operations
36+
3437
- :ref:`Additional Information <rust-crud-del-addtl-info>`
3538
provides links to resources and API documentation for types
3639
and methods mentioned in this guide
@@ -152,8 +155,40 @@ which describes the number of documents deleted. If no documents match
152155
the query filter you specified, the delete operation does
153156
not remove any documents, and the value of ``deleted_count`` is ``0``.
154157

155-
delete_many() Example
156-
~~~~~~~~~~~~~~~~~~~~~
158+
Delete Examples
159+
---------------
160+
161+
This section provides code examples for the following delete operations:
162+
163+
- :ref:`delete_one() <rust-delete-one-ex>`
164+
- :ref:`delete_many() <rust-delete-many-ex>`
165+
166+
.. _rust-delete-one-ex:
167+
168+
Delete One Example
169+
~~~~~~~~~~~~~~~~~~
170+
171+
The following example uses the ``delete_one()`` method to delete a document
172+
that has an ``item`` value of ``"placemat"``:
173+
174+
.. io-code-block::
175+
176+
.. input:: /includes/fundamentals/code-snippets/crud/delete.rs
177+
:start-after: begin-delete-one
178+
:end-before: end-delete-one
179+
:language: rust
180+
:dedent:
181+
182+
.. output::
183+
:language: console
184+
:visible: false
185+
186+
Deleted documents: 1
187+
188+
.. _rust-delete-many-ex:
189+
190+
Delete Many Example
191+
~~~~~~~~~~~~~~~~~~~
157192

158193
This example performs the following actions:
159194

@@ -163,12 +198,11 @@ This example performs the following actions:
163198
- Chains the ``hint()`` method to ``delete_many()`` to use the ``_id_`` index as the hint
164199
for the delete operation
165200

166-
167201
.. io-code-block::
168202

169203
.. input:: /includes/fundamentals/code-snippets/crud/delete.rs
170-
:start-after: begin-delete
171-
:end-before: end-delete
204+
:start-after: begin-delete-many
205+
:end-before: end-delete-many
172206
:language: rust
173207
:dedent:
174208

source/includes/fundamentals/code-snippets/crud/delete.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ async fn main() -> mongodb::error::Result<()> {
99

1010
let my_coll: Collection<Document> = client.database("db").collection("inventory");
1111

12-
// begin-delete
12+
// begin-delete-one
13+
let filter = doc! { "item": "placemat" };
14+
15+
let res = my_coll.delete_one(filter).await?;
16+
println!("Deleted documents: {}", res.deleted_count);
17+
// end-delete-one
18+
19+
// begin-delete-many
1320
let filter = doc! { "category": "garden" };
1421
let hint = Hint::Name("_id_".to_string());
1522

@@ -18,7 +25,7 @@ async fn main() -> mongodb::error::Result<()> {
1825
.hint(hint)
1926
.await?;
2027
println!("Deleted documents: {}", res.deleted_count);
21-
// end-delete
28+
// end-delete-many
2229

2330
// begin-options
2431
let res = my_coll

0 commit comments

Comments
 (0)