插入多个文档
您可以通过在 Collection
实例上调用 insert_many() 方法,将多个文档插入到集合中。
将包含一个或多个文档的向量传递给insert_many()
方法,以将它们插入到collection中。这些文档必须是您参数化Collection
实例时使用的类型的实例。 例如,如果使用 结构对collection进行了参数化,请将实例向量作为参数传递给MyStruct
MyStruct
insert_many()
方法。
提示
要插入单个文档,请考虑使用 insert_one() 方法。有关使用此方法的可运行代码示例,请参阅插入文档用法示例。
insert_many()
方法返回 InsertManyResult _id
类型,引用插入文档的 值。
要了解有关将文档插入collection的更多信息,请参阅Insert 文档指南。
例子
此示例将多个文档插入到 sample_restaurants
数据库的 restaurants
集合中。该示例通过将文档向量传递给 insert_many()
方法来插入具有 name
和 cuisine
字段值的文档。
您可以将这些文档作为 Document
类型或自定义数据类型的实例插入。要指定代表集合数据的数据类型,请对突出显示的行执行以下操作:
要访问权限集合文档并将其作为BSON文档插入,请将
<T>
类型参数替换为<Document>
,并将<struct or doc>
占位符替换为insert_docs
。要访问权限集合文档并将其作为
Restaurant
结构的实例插入,请将<T>
类型参数替换为<Restaurant>
,并将<struct or doc>
占位符替换为insert_structs
。Restaurant
结构体在代码文件的顶部定义。
选择 Asynchronous或Synchronous标签页,查看每个运行时的相应代码:
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("...")