Skip to content

Commit 4eca7e1

Browse files
committed
DOCSP-30512: insert one usage ex (#75)
(cherry picked from commit ef51d9a)
1 parent 1f3c445 commit 4eca7e1

File tree

9 files changed

+155
-12
lines changed

9 files changed

+155
-12
lines changed
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, 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 doc = Restaurant {
22+
name: "Sea Stone Tavern".to_string(),
23+
cuisine: "Greek".to_string(),
24+
borough: "Queens".to_string(),
25+
};
26+
27+
let res = my_coll.insert_one(doc, None).await?;
28+
println!("Inserted a document with _id: {}", res.inserted_id);
29+
30+
Ok(())
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 doc = Restaurant {
21+
name: "Sea Stone Tavern".to_string(),
22+
cuisine: "Greek".to_string(),
23+
borough: "Queens".to_string(),
24+
};
25+
26+
let res = my_coll.insert_one(doc, None)?;
27+
println!("Inserted a document with _id: {}", res.inserted_id);
28+
29+
Ok(())
30+
}

source/usage-examples.txt

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

1515
/usage-examples/findOne
1616
/usage-examples/find
17+
/usage-examples/insertOne
1718
/usage-examples/replace
1819
/usage-examples/deleteMany
1920
/usage-examples/count
@@ -92,12 +93,12 @@ Available Usage Examples
9293

9394
- :ref:`Find a Document <rust-find-one-usage>`
9495
- :ref:`Find Multiple Documents <rust-find-usage>`
96+
- :ref:`Insert a Document <rust-insert-one-usage>`
9597
- :ref:`Replace a Document <rust-replace-usage>`
9698
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
9799
- :ref:`Count Documents <rust-count-usage>`
98100

99101
.. TODO: add Usage Example pages as they are created
100-
- :ref:`Insert a Document <rust-insert-one-usage>`
101102
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
102103
- :ref:`Update a Document <rust-update-one-usage>`
103104
- :ref:`Update Multiple Documents <rust-update-many-usage>`

source/usage-examples/count.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ 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:
42+
4043
.. tabs::
4144

4245
.. tab:: Asynchronous
43-
:tabid: find-async
46+
:tabid: count-async
4447

4548
.. io-code-block::
4649
:copyable: true
@@ -58,7 +61,7 @@ the value of the ``name`` field includes the string ``"Sunset"``.
5861
Number of matching documents: 10
5962

6063
.. tab:: Synchronous
61-
:tabid: find-sync
64+
:tabid: count-sync
6265

6366
.. io-code-block::
6467
:copyable: true

source/usage-examples/deleteMany.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +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 corresponding code for each runtime:
39+
Select the **Asynchronous** or **Synchronous** tab to see the
40+
corresponding code for each runtime:
4041

4142
.. tabs::
4243

@@ -58,7 +59,7 @@ Select the **Asynchronous** or **Synchronous** tab to see corresponding code for
5859
Deleted documents: 615
5960

6061
.. tab:: Synchronous
61-
:tabid: find-sync
62+
:tabid: delete-many-sync
6263

6364
.. io-code-block::
6465
:copyable: true

source/usage-examples/find.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +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 corresponding code for each runtime:
36+
Select the **Asynchronous** or **Synchronous** tab to see the
37+
corresponding code for each runtime:
3738

3839
.. tabs::
3940

source/usage-examples/findOne.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ 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 corresponding code for each runtime:
32+
Select the **Asynchronous** or **Synchronous** tab to see the
33+
corresponding code for each runtime:
3334

3435
.. tabs::
3536

3637
.. tab:: Asynchronous
37-
:tabid: find-async
38+
:tabid: find-one-async
3839

3940
.. io-code-block::
4041
:copyable: true
@@ -55,7 +56,7 @@ Select the **Asynchronous** or **Synchronous** tab to see corresponding code for
5556
)
5657

5758
.. tab:: Synchronous
58-
:tabid: find-sync
59+
:tabid: find-one-sync
5960

6061
.. io-code-block::
6162
:copyable: true

source/usage-examples/insertOne.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.. _rust-insert-one-usage:
2+
3+
=================
4+
Insert a Document
5+
=================
6+
7+
You can insert a document into a collection by calling the `insert_one()
8+
<{+api+}/struct.Collection.html#method.insert_one>`__ method on a
9+
``Collection`` instance.
10+
11+
You must insert a document of the same type that you parameterized your
12+
``Collection`` instance with. For example, if you parameterized your
13+
collection with the ``MyStruct`` struct, pass a ``MyStruct``
14+
instance as a parameter to the ``insert_one()`` method to insert a
15+
document. To learn more about specifying a type parameter, see the
16+
:ref:`Collection Parameterization <rust-coll-parameterization>` section
17+
of the Databases and Collections guide.
18+
19+
The ``insert_one()`` method returns an `InsertOneResult
20+
<{+api+}/results/struct.InsertOneResult.html>`__ type which contains the
21+
``_id`` field of the newly inserted document.
22+
23+
To learn more about the ``insert_one()`` method, see the
24+
:ref:`rust-insert-guide` guide.
25+
26+
Example
27+
-------
28+
29+
This example inserts a document into the ``restaurants`` collection of
30+
the ``sample_restaurants`` database. The example uses a ``Restaurant``
31+
struct that has ``name``, ``borough``, and ``cuisine`` fields to model
32+
documents in the collection.
33+
34+
The following code creates a ``Restaurant`` instance and inserts it into
35+
the collection.
36+
37+
Select the **Asynchronous** or **Synchronous** tab to see the
38+
corresponding code for each runtime:
39+
40+
.. tabs::
41+
42+
.. tab:: Asynchronous
43+
:tabid: insert-one-async
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: /includes/usage-examples/code-snippets/insert-one-async.rs
49+
:language: rust
50+
:dedent:
51+
52+
.. output::
53+
:language: console
54+
:visible: false
55+
56+
Inserted a document with _id: ObjectId("...")
57+
58+
.. tab:: Synchronous
59+
:tabid: insert-one-sync
60+
61+
.. io-code-block::
62+
:copyable: true
63+
64+
.. input:: /includes/usage-examples/code-snippets/insert-one-sync.rs
65+
:language: rust
66+
:dedent:
67+
68+
.. output::
69+
:language: console
70+
:visible: false
71+
72+
Inserted a document with _id: ObjectId("...")

source/usage-examples/replace.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ struct that has ``name``, ``borough``, and ``cuisine`` fields to model
3232
documents in the collection.
3333

3434
The following code replaces a document in which the value of the
35-
``name`` field is ``"Landmark Coffee Shop"`` with a new document:
35+
``name`` field is ``"Landmark Coffee Shop"`` with a new document.
36+
37+
Select the **Asynchronous** or **Synchronous** tab to see the
38+
corresponding code for each runtime:
3639

3740
.. tabs::
3841

3942
.. tab:: Asynchronous
40-
:tabid: find-async
43+
:tabid: replace-async
4144

4245
.. io-code-block::
4346
:copyable: true
@@ -54,7 +57,7 @@ The following code replaces a document in which the value of the
5457
Replaced documents: 1
5558

5659
.. tab:: Synchronous
57-
:tabid: find-sync
60+
:tabid: replace-sync
5861

5962
.. io-code-block::
6063
:copyable: true

0 commit comments

Comments
 (0)