Skip to content

Commit 5a8f7ab

Browse files
committed
DOCSP-44967: Data modeling in update examples (#143)
(cherry picked from commit 734ed06)
1 parent 75e81db commit 5a8f7ab

File tree

11 files changed

+134
-26
lines changed

11 files changed

+134
-26
lines changed

source/includes/usage-examples/code-snippets/replace-async.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@ async fn main() -> mongodb::error::Result<()> {
1414
let uri = "<connection string>";
1515

1616
let client = Client::with_uri_str(uri).await?;
17-
let my_coll: Collection<Restaurant> = client
17+
18+
// Replace <T> with the <Document> or <Restaurant> type parameter
19+
let my_coll: Collection<T> = client
1820
.database("sample_restaurants")
1921
.collection("restaurants");
2022

2123
let filter = doc! { "name": "Landmark Coffee Shop" };
22-
let replacement = Restaurant {
24+
let replace_doc = doc! {
25+
"borough": "Brooklyn",
26+
"cuisine": "Café/Coffee/Tea",
27+
"name": "Harvest Moon Café",
28+
};
29+
let replace_struct = Restaurant {
2330
borough: "Brooklyn".to_string(),
2431
cuisine: "Café/Coffee/Tea".to_string(),
2532
name: "Harvest Moon Café".to_string(),
2633
};
2734

28-
let res = my_coll.replace_one(filter, replacement).await?;
35+
// Replace <struct or doc> with the replace_struct or replace_doc variable
36+
let res = my_coll.replace_one(filter, <struct or doc>).await?;
2937
println!("Replaced documents: {}", res.modified_count);
3038

3139
Ok(())

source/includes/usage-examples/code-snippets/replace-sync.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,26 @@ fn main() -> mongodb::error::Result<()> {
1313
let uri = "<connection string>";
1414

1515
let client = Client::with_uri_str(uri)?;
16-
let my_coll: Collection<Restaurant> = client
16+
17+
// Replace <T> with the <Document> or <Restaurant> type parameter
18+
let my_coll: Collection<T> = client
1719
.database("sample_restaurants")
1820
.collection("restaurants");
1921

2022
let filter = doc! { "name": "Landmark Coffee Shop" };
21-
let replacement = Restaurant {
23+
let replace_doc = doc! {
24+
"borough": "Brooklyn",
25+
"cuisine": "Café/Coffee/Tea",
26+
"name": "Harvest Moon Café",
27+
};
28+
let replace_struct = Restaurant {
2229
borough: "Brooklyn".to_string(),
2330
cuisine: "Café/Coffee/Tea".to_string(),
2431
name: "Harvest Moon Café".to_string(),
2532
};
2633

27-
let res = my_coll.replace_one(filter, replacement).run()?;
34+
// Replace <struct or doc> with the replace_struct or replace_doc variable
35+
let res = my_coll.replace_one(filter, <struct or doc>).run()?;
2836
println!("Replaced documents: {}", res.modified_count);
2937

3038
Ok(())

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
use std::env;
22
use mongodb::{ bson::doc, Client, Collection };
33
use bson::Document;
4+
use serde::{ Deserialize, Serialize };
5+
6+
#[derive(Debug, Serialize, Deserialize)]
7+
struct Address {
8+
street: String,
9+
city: String,
10+
}
11+
12+
#[derive(Serialize, Deserialize, Debug)]
13+
struct Restaurant {
14+
name: String,
15+
borough: String,
16+
address: Address,
17+
}
418

519
#[tokio::main]
620
async fn main() -> mongodb::error::Result<()> {
721
let uri = "<connection string>";
822

923
let client = Client::with_uri_str(uri).await?;
10-
let my_coll: Collection<Document> = client
24+
25+
// Replace <T> with the <Document> or <Restaurant> type parameter
26+
let my_coll: Collection<T> = client
1127
.database("sample_restaurants")
1228
.collection("restaurants");
1329

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ use mongodb::{
33
bson::{ Document, doc },
44
sync::{ Client, Collection }
55
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Debug, Serialize, Deserialize)]
9+
struct Address {
10+
street: String,
11+
city: String,
12+
}
13+
14+
#[derive(Serialize, Deserialize, Debug)]
15+
struct Restaurant {
16+
name: String,
17+
borough: String,
18+
address: Address,
19+
}
620

721
fn main() -> mongodb::error::Result<()> {
822
let uri = "<connection string>";
923

1024
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
25+
26+
// Replace <T> with the <Document> or <Restaurant> type parameter
27+
let my_coll: Collection<T> = client
1228
.database("sample_restaurants")
1329
.collection("restaurants");
1430

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ use mongodb::{
44
Client,
55
Collection
66
};
7+
use serde::{ Deserialize, Serialize };
8+
9+
#[derive(Serialize, Deserialize, Debug)]
10+
struct Restaurant {
11+
name: String,
12+
price: String,
13+
}
714

815
#[tokio::main]
916
async fn main() -> mongodb::error::Result<()> {
1017
let uri = "<connection string>";
1118

1219
let client = Client::with_uri_str(uri).await?;
13-
let my_coll: Collection<Document> = client
20+
21+
// Replace <T> with the <Document> or <Restaurant> type parameter
22+
let my_coll: Collection<T> = client
1423
.database("sample_restaurants")
1524
.collection("restaurants");
1625

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ use mongodb::{
33
bson::{ Document, doc },
44
sync::{ Client, Collection }
55
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
price: String,
12+
}
613

714
fn main() -> mongodb::error::Result<()> {
815
let uri = "<connection string>";
916

1017
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
18+
19+
// Replace <T> with the <Document> or <Restaurant> type parameter
20+
let my_coll: Collection<T> = client
1221
.database("sample_restaurants")
1322
.collection("restaurants");
1423

source/usage-examples/find.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This example retrieves documents that match a query filter from the ``restaurant
4242
collection in the ``sample_restaurants`` database. The ``find()`` method returns
4343
all documents in which the value of the ``cuisine`` field is ``"French"``.
4444

45-
You can model each retrieved document as a BSON data type or a custom data type. To specify
45+
You can model each retrieved document as a ``Document`` type or a custom data type. To specify
4646
which data type represents the collection's data, replace the ``<T>`` type parameter on the
4747
highlighted line with one of the following values:
4848

source/usage-examples/findOne.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This example retrieves a document that matches a query filter from the ``restaur
3838
collection in the ``sample_restaurants`` database. The ``find_one()`` method returns the
3939
first document in which the value of the ``name`` field is ``"Tompkins Square Bagels"``.
4040

41-
You can model the retrieved document as a BSON data type or a custom data type. To specify
41+
You can model the retrieved document as a ``Document`` type or a custom data type. To specify
4242
which data type represents the collection's data, replace the ``<T>`` type parameter on the
4343
highlighted line with one of the following values:
4444

source/usage-examples/replace.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ Example
2626
-------
2727

2828
This example replaces a document in the ``restaurants`` collection of
29-
the ``sample_restaurants`` database. The example uses a ``Restaurant``
30-
struct that has ``name``, ``borough``, and ``cuisine`` fields to model
31-
documents in the collection.
29+
the ``sample_restaurants`` database. The ``replace_one()`` method replaces
30+
the first document in which the value of the ``name`` field is
31+
``"Landmark Coffee Shop"`` with a new document.
3232

33-
The following code replaces a document in which the value of the
34-
``name`` field is ``"Landmark Coffee Shop"`` with a new document. MongoDB
35-
replaces the first document that matches the query filter.
33+
You can access the documents in the ``restaurants`` collection as instances
34+
of the ``Document`` type or a custom data type. To specify which data type represents
35+
the collection's data, perform the following actions on the highlighted lines:
36+
37+
- To access collection documents as BSON documents, replace the ``<T>`` type
38+
parameter with ``<Document>`` and the ``<struct or doc>`` placeholder with
39+
``replace_doc``.
40+
41+
- To access collection documents as instances of the ``Restaurant`` struct,
42+
replace the ``<T>`` type parameter with ``<Restaurant>`` and the ``<struct or doc>``
43+
placeholder with ``replace_struct``. The ``Restaurant`` struct is defined at
44+
the top of the code file.
3645

3746
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3847
see the corresponding code for each runtime:
@@ -47,6 +56,7 @@ see the corresponding code for each runtime:
4756

4857
.. input:: /includes/usage-examples/code-snippets/replace-async.rs
4958
:language: rust
59+
:emphasize-lines: 19, 36
5060
:dedent:
5161

5262
.. output::
@@ -63,6 +73,7 @@ see the corresponding code for each runtime:
6373

6474
.. input:: /includes/usage-examples/code-snippets/replace-sync.rs
6575
:language: rust
76+
:emphasize-lines: 18, 35
6677
:dedent:
6778

6879
.. output::

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 ``Document`` 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::

source/usage-examples/updateOne.txt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
Update a Document
55
=================
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: modify, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
720
You can update a document in a collection by calling the `update_one()
821
<{+api+}/struct.Collection.html#method.update_one>`__ method on a
922
``Collection`` instance.
@@ -26,11 +39,18 @@ Example
2639
-------
2740

2841
This example updates a document in the ``restaurants`` collection of
29-
the ``sample_restaurants`` database.
42+
the ``sample_restaurants`` database. The ``update_one()`` method adds
43+
the ``price`` field to the first document in which the value of the ``name``
44+
field is ``"Spice Market"``.
45+
46+
You can access the documents in the ``restaurants`` collection as instances
47+
of the ``Document`` type or a custom data type. To specify which data type represents
48+
the collection's data, replace the ``<T>`` type parameter on the highlighted
49+
line with one of the following values:
3050

31-
The following code adds the ``price`` field to a document in which the
32-
value of the ``name`` field is ``"Spice Market"``. MongoDB
33-
updates the first document that matches the query filter.
51+
- ``<Document>``: Accesses collection documents as BSON documents
52+
- ``<Restaurant>``: Accesses collection documents as instances of the ``Restaurant``
53+
struct, defined at the top of the code
3454

3555
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3656
see the corresponding code for each runtime:
@@ -45,6 +65,7 @@ see the corresponding code for each runtime:
4565

4666
.. input:: /includes/usage-examples/code-snippets/update-one-async.rs
4767
:language: rust
68+
:emphasize-lines: 21
4869
:dedent:
4970

5071
.. output::
@@ -61,6 +82,7 @@ see the corresponding code for each runtime:
6182

6283
.. input:: /includes/usage-examples/code-snippets/update-one-sync.rs
6384
:language: rust
85+
:emphasize-lines: 19
6486
:dedent:
6587

6688
.. output::

0 commit comments

Comments
 (0)