44Find 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+
720You 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
2841This 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
3653Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3754see 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