Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions source/fundamentals/crud/write-operations/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ This guide includes the following sections:
- :ref:`Delete Operations <rust-delete-operations>` describes how to use the
driver to execute delete operations

- :ref:`Delete Examples <rust-delete-operations>` provides code examples
for the delete operations

- :ref:`Additional Information <rust-crud-del-addtl-info>`
provides links to resources and API documentation for types
and methods mentioned in this guide
Expand Down Expand Up @@ -152,8 +155,40 @@ which describes the number of documents deleted. If no documents match
the query filter you specified, the delete operation does
not remove any documents, and the value of ``deleted_count`` is ``0``.

delete_many() Example
~~~~~~~~~~~~~~~~~~~~~
Delete Examples
---------------

This section provides code examples for the following delete operations:

- :ref:`delete_one() <rust-delete-one-ex>`
- :ref:`delete_many() <rust-delete-many-ex>`

.. _rust-delete-one-ex:

Delete One Example
~~~~~~~~~~~~~~~~~~

The following example uses the ``delete_one()`` method to delete a document
that has an ``item`` value of ``"placemat"``:

.. io-code-block::

.. input:: /includes/fundamentals/code-snippets/crud/delete.rs
:start-after: begin-delete-one
:end-before: end-delete-one
:language: rust
:dedent:

.. output::
:language: console
:visible: false

Deleted documents: 1

.. _rust-delete-many-ex:

Delete Many Example
~~~~~~~~~~~~~~~~~~~

This example performs the following actions:

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


.. io-code-block::

.. input:: /includes/fundamentals/code-snippets/crud/delete.rs
:start-after: begin-delete
:end-before: end-delete
:start-after: begin-delete-many
:end-before: end-delete-many
:language: rust
:dedent:

Expand Down
11 changes: 9 additions & 2 deletions source/includes/fundamentals/code-snippets/crud/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ async fn main() -> mongodb::error::Result<()> {

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

// begin-delete
// begin-delete-one
let filter = doc! { "item": "placemat" };

let res = my_coll.delete_one(filter).await?;
println!("Deleted documents: {}", res.deleted_count);
// end-delete-one

// begin-delete-many
let filter = doc! { "category": "garden" };
let hint = Hint::Name("_id_".to_string());

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

// begin-options
let res = my_coll
Expand Down
Loading