Skip to content

Commit c6527bc

Browse files
committed
DOCSP-30511: Find usage example (#64)
(cherry picked from commit 62f5053)
1 parent de1de5a commit c6527bc

File tree

4 files changed

+160
-3
lines changed

4 files changed

+160
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use mongodb::{
2+
bson::doc,
3+
Client,
4+
Collection
5+
};
6+
use futures::TryStreamExt;
7+
use serde::{ Deserialize, Serialize };
8+
9+
#[derive(Serialize, Deserialize, Debug)]
10+
struct Restaurant {
11+
name: String,
12+
cuisine: String,
13+
}
14+
15+
#[tokio::main]
16+
async fn main() -> mongodb::error::Result<()> {
17+
let uri = "<connection string>";
18+
let client = Client::with_uri_str(uri).await?;
19+
20+
let my_coll: Collection<Restaurant> = client
21+
.database("sample_restaurants")
22+
.collection("restaurants");
23+
24+
let mut cursor = my_coll.find(
25+
doc! { "cuisine": "French" },
26+
None
27+
).await?;
28+
29+
while let Some(doc) = cursor.try_next().await? {
30+
println!("{:?}", doc);
31+
}
32+
33+
Ok(())
34+
}
35+
36+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use mongodb::{
2+
bson::{doc, Document},
3+
sync::{Client, Collection}
4+
};
5+
use serde::{ Deserialize, Serialize };
6+
7+
#[derive(Serialize, Deserialize, Debug)]
8+
struct Restaurant {
9+
name: String,
10+
cuisine: String,
11+
}
12+
13+
fn main() -> mongodb::error::Result<()> {
14+
let uri = "<connection string>";
15+
let client = Client::with_uri_str(uri)?;
16+
17+
let my_coll: Collection<Restaurant> = client
18+
.database("sample_restaurants")
19+
.collection("restaurants");
20+
21+
let mut cursor = my_coll.find(
22+
doc! { "cuisine": "French" },
23+
None
24+
)?;
25+
26+
for result in cursor {
27+
println!("{:?}", result?);
28+
}
29+
30+
Ok(())
31+
}

source/usage-examples.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Usage Examples
1010
:depth: 1
1111
:class: singlecol
1212

13+
.. toctree::
14+
15+
/usage-examples/find
16+
1317
Overview
1418
--------
1519

@@ -77,12 +81,13 @@ up an account and a cluster, see the :atlas:`Get Started with Atlas Guide
7781
After completing these steps, you can see the output described in the **Expected Output** section
7882
of the corresponding usage example.
7983

80-
.. Available Usage Examples
81-
.. ------------------------
84+
Available Usage Examples
85+
------------------------
8286

87+
- :ref:`Find Multiple Documents <rust-find-usage>`
88+
8389
.. TODO: add Usage Example pages as they are created
8490
- :ref:`Find a Document <rust-find-one-usage>`
85-
- :ref:`Find Multiple Documents <rust-find-usage>`
8691
- :ref:`Insert a Document <rust-insert-one-usage>`
8792
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
8893
- :ref:`Update a Document <rust-update-one-usage>`

source/usage-examples/find.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.. _rust-find-usage:
2+
3+
=======================
4+
Find Multiple Documents
5+
=======================
6+
7+
You can query for multiple documents in a collection by calling the ``find()``
8+
method on a ``Collection`` instance.
9+
10+
Pass a query filter to the ``find()`` method to return documents in the collection
11+
that match the filter. If you do not include a filter, MongoDB returns all the
12+
documents in the collection.
13+
14+
The ``find()`` method returns a ``Cursor`` type, which you can iterate through
15+
to retrieve individual documents.
16+
17+
Example
18+
-------
19+
20+
This example finds documents from the ``sample_restaurants.restaurants`` collection
21+
that match a query filter. The example uses a ``Restaurant`` struct that has ``name``
22+
and ``cuisine`` fields to model the documents in the collection.
23+
24+
The following code passes a query filter as a parameter to the ``find()`` method. The
25+
filter matches documents where the value of the ``cuisine`` field is ``"French"``.
26+
27+
.. tabs::
28+
29+
.. tab:: Asynchronous
30+
:tabid: find-async
31+
32+
.. io-code-block::
33+
:copyable: true
34+
35+
.. input:: /includes/usage-examples/code-snippets/find-async.rs
36+
:language: rust
37+
:dedent:
38+
39+
.. output::
40+
:language: console
41+
:visible: false
42+
43+
// Results truncated
44+
...
45+
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
46+
Restaurant { name: "Calliope", cuisine: "French" }
47+
...
48+
49+
.. tab:: Synchronous
50+
:tabid: find-sync
51+
52+
.. io-code-block::
53+
:copyable: true
54+
55+
.. input:: /includes/usage-examples/code-snippets/find-sync.rs
56+
:language: rust
57+
:dedent:
58+
59+
.. output::
60+
:language: console
61+
:visible: false
62+
63+
// Results truncated
64+
...
65+
Restaurant { name: "Cafe Un Deux Trois", cuisine: "French" }
66+
Restaurant { name: "Calliope", cuisine: "French" }
67+
...
68+
69+
Additional Information
70+
----------------------
71+
72+
To learn more about retrieving documents, see the :ref:`rust-retrieve-guide` guide.
73+
74+
To learn more about using cursors, see the :ref:`rust-cursor-guide` guide.
75+
76+
To learn more about using query filters, see the :ref:`rust-query-guide` guide.
77+
78+
API Documentation
79+
~~~~~~~~~~~~~~~~~
80+
81+
To learn more about the methods and types mentioned on this
82+
page, see the following API documentation:
83+
84+
- `find() <{+api+}/struct.Collection.html#method.find>`__
85+
- `Cursor <{+api+}/struct.Cursor.html>`__

0 commit comments

Comments
 (0)