コレクションにドキュメントを挿入するには、 インスタンスで insert_one()Collection メソッドを呼び出します。
Collectionインスタンスをパラメータ化したのと同じタイプのドキュメントを挿入する必要があります。 たとえば、 MyStruct構造体でコレクションをパラメータ化した場合、ドキュメントを挿入するには、 MyStructインスタンスをパラメータとしてinsert_one()メソッドに渡します。 型パラメータの指定の詳細については、データベースとコレクション ガイドのコレクション パラメータ指定 セクションを参照してください。
insert_one() メソッドは、新しく挿入されたドキュメントの _idフィールドを含む InsertOneResult タイプを返します。
insert_one()メソッドについて詳しくは、ドキュメントの挿入ガイドをご覧ください。
例
この例では、 sample_restaurantsデータベースの restaurantsコレクションにドキュメントを挿入します。 insert_one() メソッドは、name、borough、cuisine のフィールド値を持つドキュメントを挿入します。
このドキュメントは、 Document 型またはカスタムデータ型のインスタンスとして挿入できます。 コレクションのデータを表すデータ型を指定するには、強調表示された行に対して次のアクションを実行します。
- コレクションドキュメントをBSONドキュメントとしてアクセスして挿入するには、 - <T>型パラメータを- <Document>に置き換え、- <struct or doc>プレースホルダーを- insert_docに置き換えます。
- Restaurant構造体のインスタンスとしてコレクションドキュメントにアクセスして挿入するには、- <T>型パラメータを- <Restaurant>に置き換え、- <struct or doc>プレースホルダーを- insert_structに置き換えます。- Restaurant構造体は、コードファイルの上部で定義されています。
AsynchronousSynchronous各実行時に対応するコードを表示するには、 タブまたは タブを選択します。
use std::env; use mongodb::{      bson::{doc, Document},     Client,     Collection  }; use serde::{ Deserialize, Serialize }; struct Restaurant {     borough: String,     cuisine: String,     name: 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_doc = doc! {          "name": "Sea Stone Tavern",         "cuisine": "Greek",         "borough": "Queens",     };     let insert_struct = Restaurant {         name: "Sea Stone Tavern".to_string(),         cuisine: "Greek".to_string(),         borough: "Queens".to_string(),     };     // Replace <struct or doc> with the insert_struct or insert_doc variable     let res = my_coll.insert_one(<struct or doc>).await?;     println!("Inserted a document with _id: {}", res.inserted_id);     Ok(()) } 
Inserted a document with _id: ObjectId("...") 
use std::env; use mongodb::{     bson::{doc, Document},     sync::{ Client, Collection } }; use serde::{ Deserialize, Serialize }; struct Restaurant {     borough: String,     cuisine: String,     name: 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_doc = doc! {          "name": "Sea Stone Tavern",         "cuisine": "Greek",         "borough": "Queens",     };     let insert_struct = Restaurant {         name: "Sea Stone Tavern".to_string(),         cuisine: "Greek".to_string(),         borough: "Queens".to_string(),     };     // Replace <struct or doc> with the insert_struct or insert_doc variable     let res = my_coll.insert_one(<struct or doc>).run()?;     println!("Inserted a document with _id: {}", res.inserted_id);     Ok(()) } 
Inserted a document with _id: ObjectId("...")