Skip to content

Commit a981d27

Browse files
authored
DOCSP-30518: Delete Many Usage Example (#67)
1 parent b63847b commit a981d27

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

source/fundamentals/database-collection.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ a database referenced by the ``db`` variable by using the
251251

252252
["my_coll", "coll_xyz", ...]
253253

254+
.. _rust-drop-collection:
255+
254256
Drop a Collection
255257
-----------------
256258

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
address: Address,
11+
borough: String,
12+
}
13+
14+
#[derive(Serialize, Deserialize, Debug)]
15+
struct Address {
16+
street: String,
17+
}
18+
19+
#[tokio::main]
20+
async fn main() -> mongodb::error::Result<()> {
21+
let uri = "<connection string>";
22+
let client = Client::with_uri_str(uri).await?;
23+
24+
let my_coll: Collection<Restaurant> = client
25+
.database("sample_restaurants")
26+
.collection("restaurants");
27+
28+
let filter =
29+
doc! { "$and": [
30+
doc! { "borough": "Manhattan" },
31+
doc! { "address.street": "Broadway" }
32+
]
33+
};
34+
35+
let result = my_coll.delete_many(filter, None).await?;
36+
37+
println!("Deleted documents: {}", result.deleted_count);
38+
39+
Ok(())
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
address: Address,
10+
borough: String,
11+
}
12+
13+
#[derive(Serialize, Deserialize, Debug)]
14+
struct Address {
15+
street: String,
16+
}
17+
18+
fn main() -> mongodb::error::Result<()> {
19+
let uri = "<connection string>";
20+
let client = Client::with_uri_str(uri)?;
21+
22+
let my_coll: Collection<Restaurant> = client
23+
.database("sample_restaurants")
24+
.collection("restaurants");
25+
26+
let filter =
27+
doc! { "$and": [
28+
doc! { "borough": "Manhattan" },
29+
doc! { "address.street": "Broadway" }
30+
]
31+
};
32+
33+
let result = my_coll.delete_many(filter, None)?;
34+
35+
println!("Deleted documents: {}", result.deleted_count);
36+
37+
Ok(())
38+
}

source/usage-examples.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage Examples
1313
.. toctree::
1414

1515
/usage-examples/find
16+
/usage-examples/deleteMany
1617
/usage-examples/count
1718

1819
Overview
@@ -88,6 +89,7 @@ Available Usage Examples
8889
------------------------
8990

9091
- :ref:`Find Multiple Documents <rust-find-usage>`
92+
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
9193
- :ref:`Count Documents <rust-count-usage>`
9294

9395
.. TODO: add Usage Example pages as they are created
@@ -98,7 +100,6 @@ Available Usage Examples
98100
- :ref:`Update Multiple Documents <rust-update-many-usage>`
99101
- :ref:`Replace a Document <rust-replace-usage>`
100102
- :ref:`Delete a Document <rust-delete-one-usage>`
101-
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
102103
- :ref:`Find Distinct Values <rust-distinct-usage>`
103104

104105

source/usage-examples/deleteMany.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _rust-delete-many-usage:
2+
3+
=========================
4+
Delete Multiple Documents
5+
=========================
6+
7+
You can delete multiple documents from a collection in a single operation
8+
by calling the `delete_many() <{+api+}/struct.Collection.html#method.delete_many>`__
9+
method on a ``Collection`` instance.
10+
11+
Pass a query filter to the ``delete_many()`` method to delete documents in the
12+
collection that match the filter. If you do not include a filter, MongoDB deletes
13+
all the documents in the collection.
14+
15+
The ``delete_many()`` method returns a `DeleteResult <{+api+}/results/struct.DeleteResult.html>`__
16+
type. This type contains information about the delete operation, such as the total
17+
number of documents deleted.
18+
19+
To learn more about delete operations, see the :ref:`rust-delete-guide` guide.
20+
21+
.. tip::
22+
23+
To delete all documents in a collection, consider calling the ``drop()``
24+
method on a ``Collection`` instance. To learn more about the ``drop()``
25+
method, see the :ref:`rust-drop-collection` section of the Databases and
26+
Collections guide.
27+
28+
Example
29+
-------
30+
31+
This example deletes all the documents from the ``restaurants`` collection that
32+
match a query filter. The example uses a ``Restaurant`` struct that has ``address``
33+
and ``borough`` fields to model the documents in the collection.
34+
35+
This example passes a query filter as a parameter to the ``delete_many()`` method.
36+
The filter matches documents where the value of the ``borough`` field is ``"Manhattan"``
37+
and the value of the ``address.street`` field is ``"Broadway"``.
38+
39+
Select the **Asynchronous** or **Synchronous** tab to see corresponding code for each runtime:
40+
41+
.. tabs::
42+
43+
.. tab:: Asynchronous
44+
:tabid: delete-many-async
45+
46+
.. io-code-block::
47+
:copyable: true
48+
49+
.. input:: /includes/usage-examples/code-snippets/delete-many-async.rs
50+
:language: rust
51+
:dedent:
52+
53+
.. output::
54+
:language: console
55+
:visible: false
56+
57+
// Your values might differ
58+
Deleted documents: 615
59+
60+
.. tab:: Synchronous
61+
:tabid: find-sync
62+
63+
.. io-code-block::
64+
:copyable: true
65+
66+
.. input:: /includes/usage-examples/code-snippets/delete-many-sync.rs
67+
:language: rust
68+
:dedent:
69+
70+
.. output::
71+
:language: console
72+
:visible: false
73+
74+
// Your values might differ
75+
Deleted documents: 615
76+

0 commit comments

Comments
 (0)