Skip to content

Commit bf6eedf

Browse files
authored
DOCSP-30510: Find One Usage Example (#68)
1 parent 2517c7e commit bf6eedf

File tree

5 files changed

+149
-9
lines changed

5 files changed

+149
-9
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 result = my_coll.find_one(
25+
doc! { "name": "Tompkins Square Bagels" },
26+
None
27+
).await?;
28+
29+
println!("{:#?}", result);
30+
31+
Ok(())
32+
}
33+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 result = my_coll.find_one(
22+
doc! { "name": "Tompkins Square Bagels" },
23+
None
24+
)?;
25+
26+
println!("{:#?}", result);
27+
28+
Ok(())
29+
}

source/usage-examples.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Usage Examples
1212

1313
.. toctree::
1414

15+
/usage-examples/findOne
1516
/usage-examples/find
1617
/usage-examples/replace
1718
/usage-examples/deleteMany
@@ -89,13 +90,13 @@ of the corresponding usage example.
8990
Available Usage Examples
9091
------------------------
9192

93+
- :ref:`Find a Document <rust-find-one-usage>`
9294
- :ref:`Find Multiple Documents <rust-find-usage>`
9395
- :ref:`Replace a Document <rust-replace-usage>`
9496
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
9597
- :ref:`Count Documents <rust-count-usage>`
9698

9799
.. TODO: add Usage Example pages as they are created
98-
- :ref:`Find a Document <rust-find-one-usage>`
99100
- :ref:`Insert a Document <rust-insert-one-usage>`
100101
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
101102
- :ref:`Update a Document <rust-update-one-usage>`

source/usage-examples/find.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ learn more about using cursors, see the :ref:`rust-cursor-guide` guide.
2525
Example
2626
-------
2727

28-
This example finds documents that match a query filter in the
29-
``restaurants`` collection of the ``sample_restaurants`` database. The
30-
example uses a ``Restaurant`` struct that has ``name`` and ``cuisine``
31-
fields to model the documents in the collection.
32-
33-
The following code passes a query filter as a parameter to the
34-
``find()`` method. The filter matches documents in which the value of
35-
the ``cuisine`` field is ``"French"``.
28+
This example retrieves documents from the ``restaurants`` collection in the
29+
``sample_restaurants`` database that match a query filter. The example uses a
30+
``Restaurant`` struct that has ``name`` and ``cuisine`` fields to model the documents
31+
in the collection.
32+
33+
The following code passes a query filter as a parameter to the ``find()`` method. The
34+
filter matches documents in which the value of the ``cuisine`` field is ``"French"``.
35+
36+
Select the **Asynchronous** or **Synchronous** tab to see corresponding code for each runtime:
3637

3738
.. tabs::
3839

source/usage-examples/findOne.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _rust-find-one-usage:
2+
3+
===============
4+
Find a Document
5+
===============
6+
7+
You can retrieve a single document in a collection by calling the `find_one()
8+
<{+api+}/struct.Collection.html#method.find_one>`__ method on a ``Collection`` instance.
9+
10+
Pass a query filter to the ``find_one()`` method to return one document in the collection
11+
that matches the filter. If multiple documents match the query filter, this method returns
12+
the first matching document according to their :term:`natural order` in the database, or
13+
according to the sort order specified in a ``FindOneOptions`` instance.
14+
15+
The ``find_one()`` method returns a `Result<Option<T>> <{+api+}/error/type.Result.html>`__
16+
type.
17+
18+
.. tip::
19+
20+
To learn more about retrieving documents, see the :ref:`rust-retrieve-guide` guide.
21+
22+
Example
23+
-------
24+
25+
This example retrieves a document that matches a query filter from the ``restaurants``
26+
collection in the ``sample_restaurants`` database. The example populates a ``Restaurant``
27+
struct with the data from the retrieved document.
28+
29+
This example uses a query filter that matches documents in which the value of the
30+
``name`` field is ``"Tompkins Square Bagels"``.
31+
32+
Select the **Asynchronous** or **Synchronous** tab to see corresponding code for each runtime:
33+
34+
.. tabs::
35+
36+
.. tab:: Asynchronous
37+
:tabid: find-async
38+
39+
.. io-code-block::
40+
:copyable: true
41+
42+
.. input:: /includes/usage-examples/code-snippets/find-one-async.rs
43+
:language: rust
44+
:dedent:
45+
46+
.. output::
47+
:language: console
48+
:visible: false
49+
50+
Some(
51+
Restaurant {
52+
name: "Tompkins Square Bagels",
53+
cuisine: "American",
54+
},
55+
)
56+
57+
.. tab:: Synchronous
58+
:tabid: find-sync
59+
60+
.. io-code-block::
61+
:copyable: true
62+
63+
.. input:: /includes/usage-examples/code-snippets/find-one-sync.rs
64+
:language: rust
65+
:dedent:
66+
67+
.. output::
68+
:language: console
69+
:visible: false
70+
71+
Some(
72+
Restaurant {
73+
name: "Tompkins Square Bagels",
74+
cuisine: "American",
75+
},
76+
)

0 commit comments

Comments
 (0)