Overview
このガイドでは、Rust ドライバーを使用して MongoDB のデータベースとコレクションにアクセスおよびコレクションを管理する方法を学習できます。
MongoDB では、データは階層構造で整理されています。 MongoDB 配置には 1 つ以上のデータベースが含まれ、各データベースには 1 つ以上のコレクションが含まれます。 各コレクションでは、MongoDB は、フィールドと値のペアを含むドキュメントとしてデータを保存します。
ドキュメント データ形式の詳細については、サーバー マニュアルのドキュメントを参照してください。
データベースへのアクセス
You can access a database by retrieving a Database instance from your client. You can use a Database instance to perform database-level operations and access collections that the database contains.
Call one of the following methods on a Client instance to create a Database:
データベース() : 名前でデータベースを検索
database_with_options() : 名前でデータベースを検索中にオプションを設定します(DatabaseOptions)
default_database(): access the default database specified for your
Clientinstance
Tip
クライアントのデフォルトのデータベースを指定するには、 ClientOptions構造体のdefault_databaseフィールドを設定します。 このフィールドを設定しない場合、ドライバーは 接続stringの defaultauthdb コンポーネントからデフォルトのデータベースを取得します。
存在しないデータベースの名前をdatabase()メソッドまたはdatabase_with_options()メソッドに渡すと、ドライバーは引き続きDatabaseインスタンスを返します。 このデータベースのコレクションにデータを挿入すると、サーバーによってデータが作成されます。
次の例では、 database()メソッドを使用してtest_dbというデータベースにアクセスします。
let db = client.database("test_db");
データベースを一覧表示する
To see a list of your deployment's databases, call the list_database_names() method on your Client instance. This method returns a Vec<String> type, a vector containing the database names as strings.
To see detailed information about each database, call the list_databases() method on your Client instance. This method returns a Vec<DatabaseSpecification> type. The DatabaseSpecification type contains fields describing each database, such as its size and whether it contains data.
次の例は、 list_database_names()メソッドを使用してデータベースのリストを出力する方法を示しています。
let db_list = client.list_database_names().await?; println!("{:?}", db_list);
データベースの削除
Dropping a database permanently deletes all the data in that database's collections. To drop a database, call the drop() method on your Database instance. The following code shows how to drop a database referenced by the db variable:
db.drop().await?;
警告
データベースの削除によるデータの削除
データベースを永続的に削除すると、データベースのコレクション内のすべてのドキュメントと、それらのコレクションのすべてのインデックスが永続的に削除されます。 データベースを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
コレクションにアクセスする
You can access a collection by retrieving a Collection instance from your database. You can use a Collection instance to perform data operations, create aggregations, and manage indexes. Call one of the following methods on a Database instance to retrieve a Collection:
コレクション() : 名前でコレクションを検索
collection_with_options() : 名前でコレクションにアクセス中にオプションを設定する(CollectionOptions)
存在しないコレクションの名前をcollection()またはcollection_with_options()メソッドに渡すと、ドライバーは引き続きCollectionインスタンスを返します。 このコレクションにデータを挿入すると、サーバーによってコレクションが作成されます。 コレクションを明示的に作成する方法については、このガイドの「 コレクションの作成 」セクションを参照してください。
この例では、 collection_with_options()メソッドを使用して次のアクションを実行しています。
db変数が参照するデータベースからcoll_xyzというコレクションにアクセスするCollectionOptionsタイプのコレクションに書込み設定(write preference)を設定する
let wc = WriteConcern::builder().journal(true).build(); let coll_opts = CollectionOptions::builder().write_concern(wc).build(); let my_coll: Collection<Document> = db.collection_with_options("coll_xyz", coll_opts);
書込み保証 (write concern) の詳細については、サーバー マニュアルの「書込み保証(write concern) 」を参照してください。
コレクション パラメータ化
コレクションのデータをシリアル化するデータ型を指定して、 Collectionインスタンスをパラメータ化する必要があります。 特定のタイプでパラメータ化されているCollectionインスタンスでメソッドを呼び出すと、メソッドはこのタイプのインスタンスを受け入れるか返します。
注意
Collectionインスタンスをパラメータ化しない場合、同じスコープ内で指定されたデータ型で CRUD 操作を実行すると、コンパイラーはジェネリック型を推測します。
次の例では、コレクションをDocument型でパラメータ化する同等の方法を示しています。
let my_coll: Collection<Document> = client.database("test_db").collection("coll_xyz"); let my_coll = client.database("test_db").collection::<Document>("coll_xyz");
Tip
Document型ではなく、データをモデル化するカスタムタイプでCollectionインスタンスをパラメータ化することをおすすめします。 特定のデータをモデル化する型を定義することで、繰り返しの直列化と検証を回避できます。
Rust ドライバーでの直列化の詳細については、「データ モデリングと直列化 に関するガイド」を参照してください。
コレクションを作成する
You can explicitly create a collection by calling the create_collection() method on a Database instance. This method takes the collection name as a parameter. You can use a Collection instance to perform data operations, create aggregations, and manage indexes.
次のコードは、 db変数が参照するデータベース内にcoll_abcというコレクションを作成する方法を示しています。
db.create_collection("coll_abc").await?;
コレクションを作成する際に、スキーマ検証を実装して一貫したドキュメント スキーマを維持し、書込み (write) 操作が検証ルールをバイパスするかどうかを制御できます。 この機能を有効にする方法については、「スキーマ検証 」のガイドを参照してください。
listCollections
To see the names of the collections in a database, call the list_collection_names() method on your Database instance. This method returns a Vec<String> type, a vector containing the collection names as strings.
To see detailed information about each collection, call the list_collections() method on your Database instance. This method returns a Vec<CollectionSpecification> type. The CollectionSpecification type contains fields describing each collection, such as its type and settings.
次の例は、 list_collection_names()メソッドを使用して、 db変数が参照するデータベース内のコレクションの名前を出力する方法を示しています。
let coll_list = db.list_collection_names().await?; println!("{:?}", coll_list);
コレクションの削除
Dropping a collection permanently deletes all the data in that collection. To drop a collection, call the drop() method on your Collection instance. The following code shows how to drop a collection referenced by the my_coll variable:
my_coll.drop().await?;
警告
コレクションを削除するとデータが削除されます
データベースからコレクションを削除すると、そのコレクション内のすべてのドキュメントとそのコレクションのすべてのインデックスが永続的に削除されます。 コレクションを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
詳細情報
このガイドの概念の詳細については、次のドキュメントを参照してください。
ドキュメントの挿入ガイド
「データベースとコレクション」のサーバー マニュアル
サーバー マニュアルのドキュメント