Skip to content

Commit 6d8f6ce

Browse files
authored
DOCSP-44969: Modify insert usage examples (#154)
1 parent eda840a commit 6d8f6ce

File tree

6 files changed

+98
-27
lines changed

6 files changed

+98
-27
lines changed

source/includes/usage-examples/code-snippets/insert-many-async.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use mongodb::{
2-
bson::doc,
2+
bson::{doc, Document},
33
Client,
44
Collection
55
};
@@ -16,11 +16,22 @@ async fn main() -> mongodb::error::Result<()> {
1616
let uri = "<connection string>";
1717
let client = Client::with_uri_str(uri).await?;
1818

19-
let my_coll: Collection<Restaurant> = client
19+
// Replace <T> with the <Document> or <Restaurant> type parameter
20+
let my_coll: Collection<T> = client
2021
.database("sample_restaurants")
2122
.collection("restaurants");
2223

23-
let docs = vec! [
24+
let insert_docs = vec! [
25+
doc! {
26+
"name": "While in Kathmandu",
27+
"cuisine": "Nepalese",
28+
},
29+
doc! {
30+
"name": "Cafe Himalaya",
31+
"cuisine": "Nepalese",
32+
}
33+
];
34+
let insert_structs = vec! [
2435
Restaurant {
2536
name: "While in Kathmandu".to_string(),
2637
cuisine: "Nepalese".to_string(),
@@ -31,7 +42,8 @@ async fn main() -> mongodb::error::Result<()> {
3142
}
3243
];
3344

34-
let insert_many_result = my_coll.insert_many(docs).await?;
45+
// Replace <structs or docs> with the insert_structs or insert_docs variable
46+
let insert_many_result = my_coll.insert_many(<structs or docs>).await?;
3547
println!("Inserted documents with _ids:");
3648
for (_key, value) in &insert_many_result.inserted_ids {
3749
println!("{}", value);

source/includes/usage-examples/code-snippets/insert-many-sync.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use mongodb::{
2-
bson::doc,
2+
bson::{doc, Document},
33
sync::{Client, Collection}
44
};
55
use serde::{ Deserialize, Serialize };
@@ -14,11 +14,22 @@ fn main() -> mongodb::error::Result<()> {
1414
let uri = "<connection string>";
1515
let client = Client::with_uri_str(uri)?;
1616

17-
let my_coll: Collection<Restaurant> = client
17+
// Replace <T> with the <Document> or <Restaurant> type parameter
18+
let my_coll: Collection<T> = client
1819
.database("sample_restaurants")
1920
.collection("restaurants");
2021

21-
let docs = vec! [
22+
let insert_docs = vec! [
23+
doc! {
24+
"name": "While in Kathmandu",
25+
"cuisine": "Nepalese",
26+
},
27+
doc! {
28+
"name": "Cafe Himalaya",
29+
"cuisine": "Nepalese",
30+
}
31+
];
32+
let insert_structs = vec! [
2233
Restaurant {
2334
name: "While in Kathmandu".to_string(),
2435
cuisine: "Nepalese".to_string(),
@@ -29,7 +40,8 @@ fn main() -> mongodb::error::Result<()> {
2940
}
3041
];
3142

32-
let insert_many_result = my_coll.insert_many(docs).run()?;
43+
// Replace <structs or docs> with the insert_structs or insert_docs variable
44+
let insert_many_result = my_coll.insert_many(<structs or docs>).run()?;
3345
println!("Inserted documents with _ids:");
3446
for (_key, value) in &insert_many_result.inserted_ids {
3547
println!("{}", value);

source/includes/usage-examples/code-snippets/insert-one-async.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use std::env;
2-
use mongodb::{ bson::doc, Client, Collection };
2+
use mongodb::{
3+
bson::{doc, Document},
4+
Client,
5+
Collection
6+
};
37
use serde::{ Deserialize, Serialize };
48

59
#[derive(Serialize, Deserialize, Debug)]
@@ -14,17 +18,25 @@ async fn main() -> mongodb::error::Result<()> {
1418
let uri = "<connection string>";
1519

1620
let client = Client::with_uri_str(uri).await?;
17-
let my_coll: Collection<Restaurant> = client
21+
22+
// Replace <T> with the <Document> or <Restaurant> type parameter
23+
let my_coll: Collection<T> = client
1824
.database("sample_restaurants")
1925
.collection("restaurants");
2026

21-
let doc = Restaurant {
27+
let insert_doc = doc! {
28+
"name": "Sea Stone Tavern",
29+
"cuisine": "Greek",
30+
"borough": "Queens",
31+
};
32+
let insert_struct = Restaurant {
2233
name: "Sea Stone Tavern".to_string(),
2334
cuisine: "Greek".to_string(),
2435
borough: "Queens".to_string(),
2536
};
2637

27-
let res = my_coll.insert_one(doc).await?;
38+
// Replace <struct or doc> with the insert_struct or insert_doc variable
39+
let res = my_coll.insert_one(<struct or doc>).await?;
2840
println!("Inserted a document with _id: {}", res.inserted_id);
2941

3042
Ok(())

source/includes/usage-examples/code-snippets/insert-one-sync.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::env;
2-
use mongodb::{ bson::doc, sync::{ Client, Collection } };
2+
use mongodb::{
3+
bson::{doc, Document},
4+
sync::{ Client, Collection }
5+
};
36
use serde::{ Deserialize, Serialize };
47

58
#[derive(Serialize, Deserialize, Debug)]
@@ -13,17 +16,25 @@ fn main() -> mongodb::error::Result<()> {
1316
let uri = "<connection string>";
1417

1518
let client = Client::with_uri_str(uri)?;
16-
let my_coll: Collection<Restaurant> = client
19+
20+
// Replace <T> with the <Document> or <Restaurant> type parameter
21+
let my_coll: Collection<T> = client
1722
.database("sample_restaurants")
1823
.collection("restaurants");
1924

20-
let doc = Restaurant {
25+
let insert_doc = doc! {
26+
"name": "Sea Stone Tavern",
27+
"cuisine": "Greek",
28+
"borough": "Queens",
29+
};
30+
let insert_struct = Restaurant {
2131
name: "Sea Stone Tavern".to_string(),
2232
cuisine: "Greek".to_string(),
2333
borough: "Queens".to_string(),
2434
};
2535

26-
let res = my_coll.insert_one(doc).run()?;
36+
// Replace <struct or doc> with the insert_struct or insert_doc variable
37+
let res = my_coll.insert_one(<struct or doc>).run()?;
2738
println!("Inserted a document with _id: {}", res.inserted_id);
2839

2940
Ok(())

source/usage-examples/insertMany.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ To learn more about inserting documents into a collection, see the
3030
Example
3131
-------
3232

33-
This example inserts documents into the ``restaurants`` collection of the
34-
``sample_restaurants`` database. The example uses a ``Restaurant`` struct containing
35-
``name`` and ``cuisine`` fields to model the documents being inserted into the
36-
collection.
33+
This example inserts multiple documents into the ``restaurants`` collection of the
34+
``sample_restaurants`` database. The example inserts documents that have
35+
``name`` and ``cuisine`` field values by passing a vector of documents
36+
to the ``insert_many()`` method.
3737

38-
This example passes a vector of documents as a parameter to the ``insert_many()``
39-
method.
38+
You can insert these documents as instances of the ``Document`` type or a
39+
custom data type. To specify which data type represents the collection's
40+
data, perform the following actions on the highlighted lines:
41+
42+
- To access and insert collection documents as BSON documents, replace the ``<T>`` type
43+
parameter with ``<Document>`` and the ``<struct or doc>`` placeholder with
44+
``insert_docs``.
45+
46+
- To access and insert collection documents as instances of the ``Restaurant`` struct,
47+
replace the ``<T>`` type parameter with ``<Restaurant>`` and the ``<struct or doc>``
48+
placeholder with ``insert_structs``. The ``Restaurant`` struct is defined at
49+
the top of the code file.
4050

4151
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
4252
see the corresponding code for each runtime:
@@ -51,6 +61,7 @@ see the corresponding code for each runtime:
5161

5262
.. input:: /includes/usage-examples/code-snippets/insert-many-async.rs
5363
:language: rust
64+
:emphasize-lines: 20, 46
5465
:dedent:
5566

5667
.. output::
@@ -69,6 +80,7 @@ see the corresponding code for each runtime:
6980

7081
.. input:: /includes/usage-examples/code-snippets/insert-many-sync.rs
7182
:language: rust
83+
:emphasize-lines: 18, 44
7284
:dedent:
7385

7486
.. output::

source/usage-examples/insertOne.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ Example
2727
-------
2828

2929
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.
30+
the ``sample_restaurants`` database. The ``insert_one()`` method
31+
inserts a document that has ``name``, ``borough``, and ``cuisine`` field
32+
values.
3333

34-
The following code creates a ``Restaurant`` instance and inserts it into
35-
the collection.
34+
You can insert this document as an instance of the ``Document`` type or a
35+
custom data type. To specify which data type represents the collection's
36+
data, perform the following actions on the highlighted lines:
37+
38+
- To access and insert collection documents as BSON documents, replace the ``<T>`` type
39+
parameter with ``<Document>`` and the ``<struct or doc>`` placeholder with
40+
``insert_doc``.
41+
42+
- To access and insert collection documents as instances of the ``Restaurant`` struct,
43+
replace the ``<T>`` type parameter with ``<Restaurant>`` and the ``<struct or doc>``
44+
placeholder with ``insert_struct``. The ``Restaurant`` struct is defined at
45+
the top of the code file.
3646

3747
Select the :guilabel:`Asynchronous` or :guilabel:`Synchronous` tab to
3848
see the corresponding code for each runtime:
@@ -47,6 +57,7 @@ see the corresponding code for each runtime:
4757

4858
.. input:: /includes/usage-examples/code-snippets/insert-one-async.rs
4959
:language: rust
60+
:emphasize-lines: 23, 39
5061
:dedent:
5162

5263
.. output::
@@ -63,6 +74,7 @@ see the corresponding code for each runtime:
6374

6475
.. input:: /includes/usage-examples/code-snippets/insert-one-sync.rs
6576
:language: rust
77+
:emphasize-lines: 21, 37
6678
:dedent:
6779

6880
.. output::

0 commit comments

Comments
 (0)