Docs Menu
Docs Home
/ / /
Rust Driver
/

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.

This example inserts documents into the restaurants collection of the sample_restaurants database. The example uses a Restaurant struct containing name and cuisine fields to model the documents being inserted into the collection.

This example passes a vector of documents as a parameter to the insert_many() method.

Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:

use mongodb::{
bson::doc,
Client,
Collection
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let docs = vec! [
Restaurant {
name: "While in Kathmandu".to_string(),
cuisine: "Nepalese".to_string(),
},
Restaurant {
name: "Cafe Himalaya".to_string(),
cuisine: "Nepalese".to_string(),
}
];
let insert_many_result = my_coll.insert_many(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,
sync::{Client, Collection}
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let docs = vec! [
Restaurant {
name: "While in Kathmandu".to_string(),
cuisine: "Nepalese".to_string(),
},
Restaurant {
name: "Cafe Himalaya".to_string(),
cuisine: "Nepalese".to_string(),
}
];
let insert_many_result = my_coll.insert_many(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("...")

Back

Insert a Document