Skip to content

Commit bd65415

Browse files
committed
find multiple
1 parent 742a27c commit bd65415

File tree

3 files changed

+95
-40
lines changed

3 files changed

+95
-40
lines changed

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

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

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

@@ -26,7 +27,7 @@ async fn main() -> mongodb::error::Result<()> {
2627
).await?;
2728

2829
while let Some(doc) = cursor.try_next().await? {
29-
println!("{:?}", doc);
30+
println!("{:#?}", doc);
3031
}
3132

3233
Ok(())

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

Lines changed: 3 additions & 2 deletions
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

@@ -23,7 +24,7 @@ fn main() -> mongodb::error::Result<()> {
2324
).run()?;
2425

2526
for result in cursor {
26-
println!("{:?}", result?);
27+
println!("{:#?}", result?);
2728
}
2829

2930
Ok(())

source/usage-examples/find.txt

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
Find Multiple Documents
55
=======================
66

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

2841
This example retrieves documents that match a query filter from the ``restaurants``
29-
collection in the ``sample_restaurants`` database. The example populates
30-
instances of the ``Restaurant`` struct with data from the retrieved
31-
documents.
42+
collection in the ``sample_restaurants`` database. The ``find()`` method returns
43+
all documents in which the value of the ``cuisine`` field is ``"French"``.
3244

33-
The following code uses a query filter that matches documents in which the value
34-
of the ``cuisine`` field is ``"French"``.
45+
You can model each retrieved document as a BSON data type or a custom data type. To specify
46+
which data type represents the collection's data, replace the ``<T>`` type parameter on the
47+
highlighted line with one of the following values:
48+
49+
- ``<Document>``: Retrieves and prints collection documents as BSON documents
50+
- ``<Restaurant>``: Retrieves and prints collection documents as instances of the ``Restaurant``
51+
struct, defined at the top of the code
3552

3653
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3754
see the corresponding code for each runtime:
3855

3956
.. tabs::
4057

4158
.. tab:: Asynchronous
42-
:tabid: find-async
43-
44-
.. io-code-block::
45-
:copyable: true
59+
:tabid: find-one-async
4660

47-
.. input:: /includes/usage-examples/code-snippets/find-async.rs
48-
:language: rust
49-
:dedent:
50-
51-
.. output::
52-
:language: console
53-
:visible: false
54-
55-
// Results truncated
56-
...
57-
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
58-
Restaurant { name: "Calliope", cuisine: "French" }
59-
...
61+
.. literalinclude:: /includes/usage-examples/code-snippets/find-async.rs
62+
:language: rust
63+
:emphasize-lines: 22
64+
:dedent:
6065

6166
.. tab:: Synchronous
62-
:tabid: find-sync
67+
:tabid: find-one-sync
6368

64-
.. io-code-block::
65-
:copyable: true
69+
.. literalinclude:: /includes/usage-examples/code-snippets/find-sync.rs
70+
:language: rust
71+
:emphasize-lines: 19
72+
:dedent:
6673

67-
.. input:: /includes/usage-examples/code-snippets/find-sync.rs
68-
:language: rust
69-
:dedent:
74+
Output
75+
~~~~~~
7076

71-
.. output::
72-
:language: console
73-
:visible: false
77+
Select the :guilabel:`BSON Document Results` or :guilabel:`Restaurant Struct Results` tab to
78+
see the corresponding code output based on your collection's type parameter:
79+
80+
.. tabs::
7481

75-
// Results truncated
76-
...
77-
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
78-
Restaurant { name: "Calliope", cuisine: "French" }
79-
...
82+
.. tab:: BSON Document Results
83+
:tabid: find-one-async
84+
85+
.. code-block:: none
86+
:copyable: false
87+
88+
...
89+
Some(
90+
Document({
91+
"_id": ObjectId(
92+
"...",
93+
),
94+
95+
...
96+
97+
"name": String(
98+
"Cafe Un Deux Trois",
99+
),
100+
"restaurant_id": String(
101+
"40369461",
102+
),
103+
}),
104+
),
105+
Some(
106+
Document({
107+
"_id": ObjectId(
108+
"...",
109+
),
110+
111+
...
112+
113+
"name": String(
114+
"Calliope",
115+
),
116+
"restaurant_id": String(
117+
"41665122",
118+
),
119+
}),
120+
)
121+
...
122+
123+
.. tab:: Restaurant Struct Results
124+
:tabid: find-one-sync
125+
126+
.. code-block:: none
127+
:copyable: false
128+
129+
...
130+
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
131+
Restaurant { name: "Calliope", cuisine: "French" }
132+
...

0 commit comments

Comments
 (0)