Skip to content

Commit ea585c9

Browse files
committed
DOCSP-30517: Delete One Usage Example (#77)
(cherry picked from commit 7d4905c)
1 parent d44a0bd commit ea585c9

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use mongodb::{
2+
bson::doc,
3+
Client,
4+
Collection
5+
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
borough: String,
12+
}
13+
14+
#[tokio::main]
15+
async fn main() -> mongodb::error::Result<()> {
16+
let uri = "<connection string>";
17+
let client = Client::with_uri_str(uri).await?;
18+
19+
let my_coll: Collection<Restaurant> = client
20+
.database("sample_restaurants")
21+
.collection("restaurants");
22+
23+
let filter =
24+
doc! { "$and": [
25+
doc! { "name": "Haagen-Dazs" },
26+
doc! { "borough": "Brooklyn" }
27+
]
28+
};
29+
30+
let result = my_coll.delete_one(filter, None).await?;
31+
32+
println!("Deleted documents: {}", result.deleted_count);
33+
34+
Ok(())
35+
}
36+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use mongodb::{
2+
bson::doc,
3+
sync::{Client, Collection}
4+
};
5+
use serde::{ Deserialize, Serialize };
6+
7+
#[derive(Serialize, Deserialize, Debug)]
8+
struct Restaurant {
9+
name: String,
10+
borough: String,
11+
}
12+
13+
fn main() -> mongodb::error::Result<()> {
14+
let uri = "<connection string>";
15+
let client = Client::with_uri_str(uri)?;
16+
17+
let my_coll: Collection<Restaurant> = client
18+
.database("sample_restaurants")
19+
.collection("restaurants");
20+
21+
let filter =
22+
doc! { "$and": [
23+
doc! { "name": "Haagen-Dazs" },
24+
doc! { "borough": "Brooklyn" }
25+
]
26+
};
27+
28+
let result = my_coll.delete_one(filter, None)?;
29+
30+
println!("Deleted documents: {}", result.deleted_count);
31+
32+
Ok(())
33+
}

source/usage-examples.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Usage Examples
1818
/usage-examples/insertMany
1919
/usage-examples/updateOne
2020
/usage-examples/replace
21+
/usage-examples/deleteOne
2122
/usage-examples/deleteMany
2223
/usage-examples/count
2324
/usage-examples/distinct
@@ -100,11 +101,10 @@ Available Usage Examples
100101
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
101102
- :ref:`Update a Document <rust-update-one-usage>`
102103
- :ref:`Replace a Document <rust-replace-usage>`
104+
- :ref:`Delete a Document <rust-delete-one-usage>`
103105
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
104106
- :ref:`Count Documents <rust-count-usage>`
105107
- :ref:`List Distinct Field Values <rust-distinct-usage>`
106108

107109
.. TODO: add Usage Example pages as they are created
108110
- :ref:`Update Multiple Documents <rust-update-many-usage>`
109-
- :ref:`Delete a Document <rust-delete-one-usage>`
110-
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`

source/usage-examples/deleteOne.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.. _rust-delete-one-usage:
2+
3+
=================
4+
Delete a Document
5+
=================
6+
7+
You can delete a document from a collection by calling the `delete_one()
8+
<{+api+}/struct.Collection.html#method.delete_one>`__ method on a ``Collection``
9+
instance.
10+
11+
Pass a query filter to the ``delete_one()`` method to match the document you want to
12+
delete from the collection. If multiple documents match the query filter, MongoDB
13+
deletes the first matching document according to their :term:`natural order` in the
14+
database or according to the sort order specified in a `DeleteOptions <{+api+}/options/struct.DeleteOptions.html>`__
15+
instance.
16+
17+
The ``delete_one()`` method returns a `DeleteResult <{+api+}/results/struct.DeleteResult.html>`__
18+
type. This type contains information about the result of the delete operation, such as
19+
the total number of documents deleted.
20+
21+
To learn more about delete operations, see the :ref:`rust-delete-guide` guide.
22+
23+
Example
24+
-------
25+
26+
This example deletes a document that matches a query filter from the ``restaurants``
27+
collection in the ``sample_restaurants`` database. The example uses a ``Restaurant`` struct
28+
that has ``name`` and ``borough`` fields to model the documents in the collection.
29+
30+
This example uses a query filter that matches documents in which the value of the
31+
``name`` field is ``"Haagen-Dazs"`` and the ``borough`` field is ``"Brooklyn"``. MongoDB
32+
deletes the first of several results that match the query filter.
33+
34+
Select the **Asynchronous** or **Synchronous** tab to see the corresponding code for
35+
each runtime:
36+
37+
.. tabs::
38+
39+
.. tab:: Asynchronous
40+
:tabid: delete-many-async
41+
42+
.. io-code-block::
43+
:copyable: true
44+
45+
.. input:: /includes/usage-examples/code-snippets/delete-one-async.rs
46+
:language: rust
47+
:dedent:
48+
49+
.. output::
50+
:language: console
51+
:visible: false
52+
53+
Deleted documents: 1
54+
55+
.. tab:: Synchronous
56+
:tabid: delete-many-sync
57+
58+
.. io-code-block::
59+
:copyable: true
60+
61+
.. input:: /includes/usage-examples/code-snippets/delete-one-sync.rs
62+
:language: rust
63+
:dedent:
64+
65+
.. output::
66+
:language: console
67+
:visible: false
68+
69+
Deleted documents: 1
70+

0 commit comments

Comments
 (0)