Insert Multiple Documents
You can insert multiple documents into a collection by calling the
insert_many() method
on a Collection
instance.
Pass a vector containing one or more documents to the insert_many()
method
to insert them into your collection. These documents must be instances of the type
that you parameterized your Collection
instance with. For example, if you
parameterized your collection with the MyStruct
struct, pass a vector of MyStruct
instances as a parameter to the insert_many()
method.
Tip
To insert a single document, consider using the insert_one() method instead. For a runnable code example that uses this method, see the Insert a Document usage example.
The insert_many()
method returns an InsertManyResult
type that references the _id
values of the inserted documents.
To learn more about inserting documents into a collection, see the Insert Documents guide.
Example
This example inserts multiple documents into the restaurants
collection of the
sample_restaurants
database. The example inserts documents that have
name
and cuisine
field values by passing a vector of documents
to the insert_many()
method.
You can insert these documents as instances of the Document
type or a
custom data type. To specify which data type represents the collection's
data, perform the following actions on the highlighted lines:
To access and insert collection documents as BSON documents, replace the
<T>
type parameter with<Document>
and the<struct or doc>
placeholder withinsert_docs
.To access and insert collection documents as instances of the
Restaurant
struct, replace the<T>
type parameter with<Restaurant>
and the<struct or doc>
placeholder withinsert_structs
. TheRestaurant
struct is defined at the top of the code file.
Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
use mongodb::{ bson::{doc, Document}, Client, Collection }; use serde::{ Deserialize, Serialize }; struct Restaurant { name: String, cuisine: String, } async fn main() -> mongodb::error::Result<()> { let uri = "<connection string>"; let client = Client::with_uri_str(uri).await?; // Replace <T> with the <Document> or <Restaurant> type parameter let my_coll: Collection<T> = client .database("sample_restaurants") .collection("restaurants"); let insert_docs = vec! [ doc! { "name": "While in Kathmandu", "cuisine": "Nepalese", }, doc! { "name": "Cafe Himalaya", "cuisine": "Nepalese", } ]; let insert_structs = vec! [ Restaurant { name: "While in Kathmandu".to_string(), cuisine: "Nepalese".to_string(), }, Restaurant { name: "Cafe Himalaya".to_string(), cuisine: "Nepalese".to_string(), } ]; // Replace <structs or docs> with the insert_structs or insert_docs variable let insert_many_result = my_coll.insert_many(<structs or docs>).await?; println!("Inserted documents with _ids:"); for (_key, value) in &insert_many_result.inserted_ids { println!("{}", value); } Ok(()) }
Inserted documents with _ids: ObjectId("...") ObjectId("...")
use mongodb::{ bson::{doc, Document}, sync::{Client, Collection} }; use serde::{ Deserialize, Serialize }; struct Restaurant { name: String, cuisine: String, } fn main() -> mongodb::error::Result<()> { let uri = "<connection string>"; let client = Client::with_uri_str(uri)?; // Replace <T> with the <Document> or <Restaurant> type parameter let my_coll: Collection<T> = client .database("sample_restaurants") .collection("restaurants"); let insert_docs = vec! [ doc! { "name": "While in Kathmandu", "cuisine": "Nepalese", }, doc! { "name": "Cafe Himalaya", "cuisine": "Nepalese", } ]; let insert_structs = vec! [ Restaurant { name: "While in Kathmandu".to_string(), cuisine: "Nepalese".to_string(), }, Restaurant { name: "Cafe Himalaya".to_string(), cuisine: "Nepalese".to_string(), } ]; // Replace <structs or docs> with the insert_structs or insert_docs variable let insert_many_result = my_coll.insert_many(<structs or docs>).run()?; println!("Inserted documents with _ids:"); for (_key, value) in &insert_many_result.inserted_ids { println!("{}", value); } Ok(()) }
Inserted documents with _ids: ObjectId("...") ObjectId("...")