Skip to content

Commit 74b66ca

Browse files
committed
DOCSP-44967: Data modeling in update examples
1 parent 179c64e commit 74b66ca

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ use mongodb::{
55
Collection
66
};
77

8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
price: String,
12+
}
13+
814
#[tokio::main]
915
async fn main() -> mongodb::error::Result<()> {
1016
let uri = "<connection string>";
1117

1218
let client = Client::with_uri_str(uri).await?;
13-
let my_coll: Collection<Document> = client
19+
20+
// Replace <T> with the <Document> or <Restaurant> type parameter
21+
let my_coll: Collection<T> = client
1422
.database("sample_restaurants")
1523
.collection("restaurants");
1624

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

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

7+
#[derive(Serialize, Deserialize, Debug)]
8+
struct Restaurant {
9+
name: String,
10+
price: String,
11+
}
12+
713
fn main() -> mongodb::error::Result<()> {
814
let uri = "<connection string>";
915

1016
let client = Client::with_uri_str(uri)?;
11-
let my_coll: Collection<Document> = client
17+
18+
// Replace <T> with the <Document> or <Restaurant> type parameter
19+
let my_coll: Collection<T> = client
1220
.database("sample_restaurants")
1321
.collection("restaurants");
1422

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 BSON data 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)