Skip to content

Commit 9aa59a1

Browse files
committed
Merge remote-tracking branch 'upstream/master' into v2.7
2 parents 843a96b + 2517c7e commit 9aa59a1

File tree

8 files changed

+302
-4
lines changed

8 files changed

+302
-4
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, Client, Collection };
3+
use serde::{ Deserialize, Serialize };
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
struct Restaurant {
7+
borough: String,
8+
cuisine: String,
9+
name: String,
10+
}
11+
12+
#[tokio::main]
13+
async fn main() -> mongodb::error::Result<()> {
14+
let uri = "<connection string>";
15+
16+
let client = Client::with_uri_str(uri).await?;
17+
let my_coll: Collection<Restaurant> = client
18+
.database("sample_restaurants")
19+
.collection("restaurants");
20+
21+
let filter = doc! { "name": "Landmark Coffee Shop" };
22+
let replacement = Restaurant {
23+
borough: "Brooklyn".to_string(),
24+
cuisine: "Café/Coffee/Tea".to_string(),
25+
name: "Harvest Moon Café".to_string(),
26+
};
27+
28+
let res = my_coll.replace_one(filter, replacement, None).await?;
29+
println!(
30+
"Matched documents: {}\nReplaced documents: {}",
31+
res.matched_count,
32+
res.modified_count
33+
);
34+
35+
Ok(())
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, sync::{ Client, Collection } };
3+
use serde::{ Deserialize, Serialize };
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
struct Restaurant {
7+
borough: String,
8+
cuisine: String,
9+
name: String,
10+
}
11+
12+
fn main() -> mongodb::error::Result<()> {
13+
let uri = "<connection string>";
14+
15+
let client = Client::with_uri_str(uri)?;
16+
let my_coll: Collection<Restaurant> = client
17+
.database("sample_restaurants")
18+
.collection("restaurants");
19+
20+
let filter = doc! { "name": "Landmark Coffee Shop" };
21+
let replacement = Restaurant {
22+
borough: "Brooklyn".to_string(),
23+
cuisine: "Café/Coffee/Tea".to_string(),
24+
name: "Harvest Moon Café".to_string(),
25+
};
26+
27+
let res = my_coll.replace_one(filter, replacement, None)?;
28+
println!(
29+
"Matched documents: {}\nReplaced documents: {}",
30+
res.matched_count,
31+
res.modified_count
32+
);
33+
34+
Ok(())
35+
}

source/usage-examples.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Usage Examples
1313
.. toctree::
1414

1515
/usage-examples/find
16+
/usage-examples/replace
17+
/usage-examples/deleteMany
1618
/usage-examples/count
1719

1820
Overview
@@ -88,6 +90,8 @@ Available Usage Examples
8890
------------------------
8991

9092
- :ref:`Find Multiple Documents <rust-find-usage>`
93+
- :ref:`Replace a Document <rust-replace-usage>`
94+
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
9195
- :ref:`Count Documents <rust-count-usage>`
9296

9397
.. TODO: add Usage Example pages as they are created
@@ -96,9 +100,5 @@ Available Usage Examples
96100
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
97101
- :ref:`Update a Document <rust-update-one-usage>`
98102
- :ref:`Update Multiple Documents <rust-update-many-usage>`
99-
- :ref:`Replace a Document <rust-replace-usage>`
100103
- :ref:`Delete a Document <rust-delete-one-usage>`
101-
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
102104
- :ref:`Find Distinct Values <rust-distinct-usage>`
103-
104-

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+

source/usage-examples/replace.txt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _rust-replace-usage:
2+
3+
==================
4+
Replace a Document
5+
==================
6+
7+
You can replace a document in a collection by calling the `replace_one()
8+
<{+api+}/struct.Collection.html#method.replace_one>`__ method on a
9+
``Collection`` instance.
10+
11+
Pass the following parameters to the ``replace_one()`` method to update
12+
a document:
13+
14+
- Query filter, which specifies the criteria to match
15+
- Replacement document, which contains the fields and values that will
16+
replace an existing document
17+
18+
The ``replace_one()`` method returns an `UpdateResult
19+
<{+api+}/results/struct.UpdateResult.html>`__ type which contains
20+
information about the result of the replace operation, such as the
21+
number of modified documents.
22+
23+
To learn more about the ``replace_one()`` method, see the
24+
:ref:`rust-replace-document` section of the Modify Documents guide.
25+
26+
Example
27+
-------
28+
29+
This example replaces a document in the ``restaurants`` collection of
30+
the ``sample_restaurants`` database. The example uses a ``Restaurant``
31+
struct that has ``name``, ``borough``, and ``cuisine`` fields to model
32+
documents in the collection.
33+
34+
The following code replaces a document in which the value of the
35+
``name`` field is ``"Landmark Coffee Shop"`` with a new document:
36+
37+
.. tabs::
38+
39+
.. tab:: Asynchronous
40+
:tabid: find-async
41+
42+
.. io-code-block::
43+
:copyable: true
44+
45+
.. input:: /includes/usage-examples/code-snippets/replace-async.rs
46+
:language: rust
47+
:dedent:
48+
49+
.. output::
50+
:language: console
51+
:visible: false
52+
53+
Matched documents: 1
54+
Replaced documents: 1
55+
56+
.. tab:: Synchronous
57+
:tabid: find-sync
58+
59+
.. io-code-block::
60+
:copyable: true
61+
62+
.. input:: /includes/usage-examples/code-snippets/replace-sync.rs
63+
:language: rust
64+
:dedent:
65+
66+
.. output::
67+
:language: console
68+
:visible: false
69+
70+
Matched documents: 1
71+
Replaced documents: 1

0 commit comments

Comments
 (0)