Skip to content

Commit 91e49ad

Browse files
committed
DOCSP-44853: Data modeling in usage examples
1 parent 8f48bf7 commit 91e49ad

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ async fn main() -> mongodb::error::Result<()> {
1616
let uri = "<connection string>";
1717
let client = Client::with_uri_str(uri).await?;
1818

19-
let my_coll: Collection<Restaurant> = client
19+
// Replace <T> with the <Document> or <Restaurant> type parameter
20+
let my_coll: Collection<T> = client
2021
.database("sample_restaurants")
2122
.collection("restaurants");
2223

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fn main() -> mongodb::error::Result<()> {
1414
let uri = "<connection string>";
1515
let client = Client::with_uri_str(uri)?;
1616

17-
let my_coll: Collection<Restaurant> = client
17+
// Replace <T> with the <Document> or <Restaurant> type parameter
18+
let my_coll: Collection<T> = client
1819
.database("sample_restaurants")
1920
.collection("restaurants");
2021

source/usage-examples/findOne.txt

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ Example
2222
-------
2323

2424
This example retrieves a document that matches a query filter from the ``restaurants``
25-
collection in the ``sample_restaurants`` database. The example populates a ``Restaurant``
26-
struct with data from the retrieved document.
25+
collection in the ``sample_restaurants`` database. The ``find_one()`` method returns the
26+
first document in which the value of the ``name`` field is ``"Tompkins Square Bagels"``.
2727

28-
This example uses a query filter that matches documents in which the value of the
29-
``name`` field is ``"Tompkins Square Bagels"``. MongoDB retrieves the
30-
first document that matches the query filter.
28+
You can model the retrieved document as a BSON data type or a custom data type. To specify
29+
which data type models the collection's data, replace the ``<T>`` type parameter on the
30+
highlighted line with one of the following values:
31+
32+
- ``<Document>``: Retrieves and prints collection documents as BSON documents
33+
- ``<Restaurant>``: Retrieves and prints collection documents as instances of the ``Restaurant``
34+
struct, defined at the top of the code
3135

3236
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3337
see the corresponding code for each runtime:
@@ -37,41 +41,59 @@ see the corresponding code for each runtime:
3741
.. tab:: Asynchronous
3842
:tabid: find-one-async
3943

40-
.. io-code-block::
41-
:copyable: true
44+
.. literalinclude:: /includes/usage-examples/code-snippets/find-one-async.rs
45+
:language: rust
46+
:emphasize-lines: 19
47+
:dedent:
4248

43-
.. input:: /includes/usage-examples/code-snippets/find-one-async.rs
44-
:language: rust
45-
:dedent:
49+
.. tab:: Synchronous
50+
:tabid: find-one-sync
4651

47-
.. output::
48-
:language: console
49-
:visible: false
52+
.. literalinclude:: /includes/usage-examples/code-snippets/find-one-sync.rs
53+
:language: rust
54+
:emphasize-lines: 17
55+
:dedent:
5056

51-
Some(
52-
Restaurant {
53-
name: "Tompkins Square Bagels",
54-
cuisine: "American",
55-
},
56-
)
57+
Output
58+
~~~~~~
5759

58-
.. tab:: Synchronous
59-
:tabid: find-one-sync
60+
Select the :guilabel:`BSON Document Result` or :guilabel:`Restaurant Struct Result` tab to
61+
see the corresponding code output based on your collection's type parameter:
6062

61-
.. io-code-block::
62-
:copyable: true
63+
.. tabs::
6364

64-
.. input:: /includes/usage-examples/code-snippets/find-one-sync.rs
65-
:language: rust
66-
:dedent:
65+
.. tab:: BSON Document Result
66+
:tabid: find-one-async
67+
68+
.. code-block:: none
69+
:copyable: false
70+
71+
Some(
72+
Document({
73+
"_id": ObjectId(
74+
"...",
75+
),
76+
77+
...
78+
79+
"name": String(
80+
"Tompkins Square Bagels",
81+
),
82+
"restaurant_id": String(
83+
"41605054",
84+
),
85+
}),
86+
)
87+
88+
.. tab:: Restaurant Struct Result
89+
:tabid: find-one-sync
6790

68-
.. output::
69-
:language: console
70-
:visible: false
91+
.. code-block:: none
92+
:copyable: false
7193

72-
Some(
73-
Restaurant {
74-
name: "Tompkins Square Bagels",
75-
cuisine: "American",
76-
},
77-
)
94+
Some(
95+
Restaurant {
96+
name: "Tompkins Square Bagels",
97+
cuisine: "American",
98+
},
99+
)

0 commit comments

Comments
 (0)