Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Rust ドライバー
/

データベースとコレクション

このガイドでは、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:

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);
["admin", "local", "test_db", ...]

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()または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) 操作が検証ルールをバイパスするかどうかを制御できます。 この機能を有効にする方法については、「スキーマ検証 」のガイドを参照してください。

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);
["my_coll", "coll_xyz", ...]

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?;

警告

コレクションを削除するとデータが削除されます

データベースからコレクションを削除すると、そのコレクション内のすべてのドキュメントとそのコレクションのすべてのインデックスが永続的に削除されます。 コレクションを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。

このガイドの概念の詳細については、次のドキュメントを参照してください。

戻る

直列化

項目一覧