Skip to content

Commit f27fc84

Browse files
committed
DOCSP-30514: update one usage example (#78)
(cherry picked from commit e782270)
1 parent 1f8e58f commit f27fc84

File tree

11 files changed

+155
-17
lines changed

11 files changed

+155
-17
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, Client, Collection };
3+
use serde::{ Deserialize, Serialize };
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
struct Restaurant {
7+
borough: String,
8+
cuisine: String,
9+
name: String,
10+
}
11+
12+
#[tokio::main]
13+
async fn main() -> mongodb::error::Result<()> {
14+
let uri = "<connection string>";
15+
16+
let client = Client::with_uri_str(uri).await?;
17+
let my_coll: Collection<Restaurant> = client
18+
.database("sample_restaurants")
19+
.collection("restaurants");
20+
21+
let filter = doc! { "name": "Spice Market" };
22+
let update = doc! { "$set": doc! {"price": "$$$"} };
23+
24+
let res = my_coll.update_one(filter, update, None).await?;
25+
println!(
26+
"Matched documents: {}\nUpdated documents: {}",
27+
res.matched_count,
28+
res.modified_count
29+
);
30+
31+
Ok(())
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::env;
2+
use mongodb::{ bson::doc, sync::{ Client, Collection } };
3+
use serde::{ Deserialize, Serialize };
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
struct Restaurant {
7+
borough: String,
8+
cuisine: String,
9+
name: String,
10+
}
11+
12+
fn main() -> mongodb::error::Result<()> {
13+
let uri = "<connection string>";
14+
15+
let client = Client::with_uri_str(uri)?;
16+
let my_coll: Collection<Restaurant> = client
17+
.database("sample_restaurants")
18+
.collection("restaurants");
19+
20+
let filter = doc! { "name": "Spice Market" };
21+
let update = doc! { "$set": doc! {"price": "$$$"} };
22+
23+
let res = my_coll.update_one(filter, update, None)?;
24+
println!(
25+
"Matched documents: {}\nUpdated documents: {}",
26+
res.matched_count,
27+
res.modified_count
28+
);
29+
30+
Ok(())
31+
}

source/op-error-handling.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Connection Errors
7676

7777
A concurrent operation error might clear the connection pool,
7878
interrupting your connection to the server. In this
79-
situation, the driver raises an ``Error`` type where the value of the
79+
situation, the driver raises an ``Error`` type in which the value of the
8080
``kind`` field is ``ConnectionPoolCleared``. The error message describes
8181
the reason that the concurrent operation failed.
8282

source/usage-examples.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Usage Examples
1515
/usage-examples/findOne
1616
/usage-examples/find
1717
/usage-examples/insertOne
18+
/usage-examples/insertMany
19+
/usage-examples/updateOne
1820
/usage-examples/replace
1921
/usage-examples/deleteMany
2022
/usage-examples/count
@@ -95,14 +97,14 @@ Available Usage Examples
9597
- :ref:`Find a Document <rust-find-one-usage>`
9698
- :ref:`Find Multiple Documents <rust-find-usage>`
9799
- :ref:`Insert a Document <rust-insert-one-usage>`
100+
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
101+
- :ref:`Update a Document <rust-update-one-usage>`
98102
- :ref:`Replace a Document <rust-replace-usage>`
99103
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
100104
- :ref:`Count Documents <rust-count-usage>`
101105
- :ref:`List Distinct Field Values <rust-distinct-usage>`
102106

103107
.. TODO: add Usage Example pages as they are created
104-
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
105-
- :ref:`Update a Document <rust-update-one-usage>`
106108
- :ref:`Update Multiple Documents <rust-update-many-usage>`
107109
- :ref:`Delete a Document <rust-delete-one-usage>`
108110
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`

source/usage-examples/count.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ example uses the ``count_documents()`` method to count the number of
3737
documents that match a query filter. The filter matches documents in which
3838
the value of the ``name`` field includes the string ``"Sunset"``:
3939

40-
Select the **Asynchronous** or **Synchronous** tab to see the
41-
corresponding code for each runtime:
40+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
41+
see the corresponding code for each runtime:
4242

4343
.. tabs::
4444

source/usage-examples/deleteMany.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ This example passes a query filter as a parameter to the ``delete_many()`` metho
3636
The filter matches documents where the value of the ``borough`` field is ``"Manhattan"``
3737
and the value of the ``address.street`` field is ``"Broadway"``.
3838

39-
Select the **Asynchronous** or **Synchronous** tab to see the
40-
corresponding code for each runtime:
39+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
40+
see the corresponding code for each runtime:
4141

4242
.. tabs::
4343

source/usage-examples/find.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ in the collection.
3333
The following code passes a query filter as a parameter to the ``find()`` method. The
3434
filter matches documents in which the value of the ``cuisine`` field is ``"French"``.
3535

36-
Select the **Asynchronous** or **Synchronous** tab to see the
37-
corresponding code for each runtime:
36+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
37+
see the corresponding code for each runtime:
3838

3939
.. tabs::
4040

source/usage-examples/findOne.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct with the data from the retrieved document.
2929
This example uses a query filter that matches documents in which the value of the
3030
``name`` field is ``"Tompkins Square Bagels"``.
3131

32-
Select the **Asynchronous** or **Synchronous** tab to see the
33-
corresponding code for each runtime:
32+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
33+
see the corresponding code for each runtime:
3434

3535
.. tabs::
3636

source/usage-examples/insertOne.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ document. To learn more about specifying a type parameter, see the
1717
of the Databases and Collections guide.
1818

1919
The ``insert_one()`` method returns an `InsertOneResult
20-
<{+api+}/results/struct.InsertOneResult.html>`__ type which contains the
20+
<{+api+}/results/struct.InsertOneResult.html>`__ type that contains the
2121
``_id`` field of the newly inserted document.
2222

2323
To learn more about the ``insert_one()`` method, see the
@@ -34,8 +34,8 @@ documents in the collection.
3434
The following code creates a ``Restaurant`` instance and inserts it into
3535
the collection.
3636

37-
Select the **Asynchronous** or **Synchronous** tab to see the
38-
corresponding code for each runtime:
37+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
38+
see the corresponding code for each runtime:
3939

4040
.. tabs::
4141

source/usage-examples/replace.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ a document:
1616
replace an existing document
1717

1818
The ``replace_one()`` method returns an `UpdateResult
19-
<{+api+}/results/struct.UpdateResult.html>`__ type which contains
19+
<{+api+}/results/struct.UpdateResult.html>`__ type that contains
2020
information about the result of the replace operation, such as the
2121
number of modified documents.
2222

@@ -34,8 +34,8 @@ documents in the collection.
3434
The following code replaces a document in which the value of the
3535
``name`` field is ``"Landmark Coffee Shop"`` with a new document.
3636

37-
Select the **Asynchronous** or **Synchronous** tab to see the
38-
corresponding code for each runtime:
37+
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
38+
see the corresponding code for each runtime:
3939

4040
.. tabs::
4141

0 commit comments

Comments
 (0)