Skip to content

Commit 6baab30

Browse files
committed
update many
1 parent 74b66ca commit 6baab30

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

source/includes/usage-examples/code-snippets/update-many-async.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ use std::env;
22
use mongodb::{ bson::doc, Client, Collection };
33
use bson::Document;
44

5+
#[derive(Debug, Serialize, Deserialize)]
6+
struct Address {
7+
street: String,
8+
city: String,
9+
}
10+
11+
#[derive(Serialize, Deserialize, Debug)]
12+
struct Restaurant {
13+
name: String,
14+
borough: String,
15+
address: Address,
16+
}
17+
518
#[tokio::main]
619
async fn main() -> mongodb::error::Result<()> {
720
let uri = "<connection string>";
821

922
let client = Client::with_uri_str(uri).await?;
10-
let my_coll: Collection<Document> = client
23+
24+
// Replace <T> with the <Document> or <Restaurant> type parameter
25+
let my_coll: Collection<T> = client
1126
.database("sample_restaurants")
1227
.collection("restaurants");
1328

source/includes/usage-examples/code-snippets/update-many-sync.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@ use mongodb::{
44
sync::{ Client, Collection }
55
};
66

7+
#[derive(Debug, Serialize, Deserialize)]
8+
struct Address {
9+
street: String,
10+
city: String,
11+
}
12+
13+
#[derive(Serialize, Deserialize, Debug)]
14+
struct Restaurant {
15+
name: String,
16+
borough: String,
17+
address: Address,
18+
}
19+
720
fn main() -> mongodb::error::Result<()> {
821
let uri = "<connection string>";
922

1023
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
24+
25+
// Replace <T> with the <Document> or <Restaurant> type parameter
26+
let my_coll: Collection<T> = client
1227
.database("sample_restaurants")
1328
.collection("restaurants");
1429

source/usage-examples/updateMany.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ Example
2626
-------
2727

2828
This example updates a document in the ``restaurants`` collection of
29-
the ``sample_restaurants`` database.
29+
the ``sample_restaurants`` database. The ``update_many()`` method adds
30+
the ``near_me`` field to documents in which the value of the ``address.street``
31+
field is ``"Sullivan Street"`` and the ``borough`` field is ``"Manhattan"``.
3032

31-
The following code adds the ``near_me`` field to documents in which the
32-
value of the ``address.street`` field is ``"Sullivan Street"`` and the
33-
``borough`` field is ``"Manhattan"``.
33+
You can access the documents in the ``restaurants`` collection as instances
34+
of the BSON data type or a custom data type. To specify which data type represents
35+
the collection's data, replace the ``<T>`` type parameter on the highlighted
36+
line with one of the following values:
37+
38+
- ``<Document>``: Accesses collection documents as BSON documents.
39+
- ``<Restaurant>``: Accesses collection documents as instances of the ``Restaurant``
40+
struct, defined at the top of the code
3441

3542
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3643
see the corresponding code for each runtime:
@@ -45,6 +52,7 @@ see the corresponding code for each runtime:
4552

4653
.. input:: /includes/usage-examples/code-snippets/update-many-async.rs
4754
:language: rust
55+
:emphasize-lines: 25
4856
:dedent:
4957

5058
.. output::
@@ -62,6 +70,7 @@ see the corresponding code for each runtime:
6270

6371
.. input:: /includes/usage-examples/code-snippets/update-many-sync.rs
6472
:language: rust
73+
:emphasize-lines: 26
6574
:dedent:
6675

6776
.. output::

0 commit comments

Comments
 (0)