Skip to content

Commit e4f948e

Browse files
norareidyrustagir
authored andcommitted
DOCSP-30513: Insert Many Usage Example (#74)
(cherry picked from commit 9e6416c)
1 parent f27fc84 commit e4f948e

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use mongodb::{
2+
bson::doc,
3+
Client,
4+
Collection
5+
};
6+
use serde::{ Deserialize, Serialize };
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
struct Restaurant {
10+
name: String,
11+
cuisine: String,
12+
}
13+
14+
#[tokio::main]
15+
async fn main() -> mongodb::error::Result<()> {
16+
let uri = "<connection string>";
17+
let client = Client::with_uri_str(uri).await?;
18+
19+
let my_coll: Collection<Restaurant> = client
20+
.database("sample_restaurants")
21+
.collection("restaurants");
22+
23+
let docs = vec! [
24+
Restaurant {
25+
name: "While in Kathmandu".to_string(),
26+
cuisine: "Nepalese".to_string(),
27+
},
28+
Restaurant {
29+
name: "Cafe Himalaya".to_string(),
30+
cuisine: "Nepalese".to_string(),
31+
}
32+
];
33+
34+
let insert_many_result = my_coll.insert_many(docs, None).await?;
35+
println!("Inserted documents with _ids:");
36+
for (_key, value) in &insert_many_result.inserted_ids {
37+
println!("{}", value);
38+
}
39+
40+
Ok(())
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use mongodb::{
2+
bson::doc,
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 docs = vec! [
22+
Restaurant {
23+
name: "While in Kathmandu".to_string(),
24+
cuisine: "Nepalese".to_string(),
25+
},
26+
Restaurant {
27+
name: "Cafe Himalaya".to_string(),
28+
cuisine: "Nepalese".to_string(),
29+
}
30+
];
31+
32+
let insert_many_result = my_coll.insert_many(docs, None)?;
33+
println!("Inserted documents with _ids:");
34+
for (_key, value) in &insert_many_result.inserted_ids {
35+
println!("{}", value);
36+
}
37+
38+
Ok(())
39+
}

source/usage-examples/insertMany.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. _rust-insert-many-usage:
2+
3+
=========================
4+
Insert Multiple Documents
5+
=========================
6+
7+
You can insert multiple documents into a collection by calling the
8+
`insert_many() <{+api+}/struct.Collection.html#method.insert_many>`__ method
9+
on a ``Collection`` instance.
10+
11+
Pass a vector containing one or more documents to the ``insert_many()`` method
12+
to insert them into your collection. These documents must be instances of the type
13+
that you parameterized your ``Collection`` instance with. For example, if you
14+
parameterized your collection with the ``MyStruct`` struct, pass a vector of ``MyStruct``
15+
instances as a parameter to the ``insert_many()`` method.
16+
17+
.. tip::
18+
19+
To insert a single document, consider using the `insert_one()
20+
<{+api+}/struct.Collection.html#method.insert_one>`__ method instead. For a
21+
runnable code example that uses this method, see the :ref:`rust-insert-one-usage`
22+
usage example.
23+
24+
The ``insert_many()`` method returns an `InsertManyResult <{+api+}/results/struct.InsertManyResult.html>`__
25+
type that references the ``_id`` values of the inserted documents.
26+
27+
To learn more about inserting documents into a collection, see the
28+
:ref:`rust-insert-guide` guide.
29+
30+
Example
31+
-------
32+
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.
37+
38+
This example passes a vector of documents as a parameter to the ``insert_many()``
39+
method.
40+
41+
Select the **Asynchronous** or **Synchronous** tab to see corresponding code for each runtime:
42+
43+
.. tabs::
44+
45+
.. tab:: Asynchronous
46+
:tabid: find-async
47+
48+
.. io-code-block::
49+
:copyable: true
50+
51+
.. input:: /includes/usage-examples/code-snippets/insert-many-async.rs
52+
:language: rust
53+
:dedent:
54+
55+
.. output::
56+
:language: console
57+
:visible: false
58+
59+
Inserted documents with _ids:
60+
ObjectId("...")
61+
ObjectId("...")
62+
63+
.. tab:: Synchronous
64+
:tabid: find-sync
65+
66+
.. io-code-block::
67+
:copyable: true
68+
69+
.. input:: /includes/usage-examples/code-snippets/insert-many-sync.rs
70+
:language: rust
71+
:dedent:
72+
73+
.. output::
74+
:language: console
75+
:visible: false
76+
77+
Inserted documents with _ids:
78+
ObjectId("...")
79+
ObjectId("...")

0 commit comments

Comments
 (0)