Overview
このガイドでは、.NET/C# ドライバーを使用して MongoDB のデータベースとコレクションにアクセスおよび管理する方法を学習できます。
MongoDB では、データは階層構造で整理されています。 MongoDB 配置には 1 つ以上のデータベースが含まれ、各データベースには 1 つ以上のコレクションが含まれます。 各コレクションでは、MongoDB は、フィールドと値のペアを含むドキュメントとしてデータを保存します。
ドキュメント データ形式の詳細については、サーバー マニュアルのドキュメントを参照してください。
データベースへのアクセス
データベースにアクセスするには、 インスタンスから IMongoDatabaseインスタンスを取得します。返されたIMongoClient
IMongoDatabase
インスタンスを使用して、データベースレベルの操作を実行し、データベースに含まれるコレクションにアクセスできます。
を作成するには、データベース名をパラメータとして渡して、 IMongoDatabase
IMongoClientインスタンスで GetDatabase() メソッドを呼び出します。また、任意の MongoDatabaseSettings をパラメーターとして渡して、データベースへのアクセス方法をカスタマイズできます。
存在しないデータベースの名前をGetDatabase()
メソッドに渡すと、ドライバーは引き続きIMongoDatabase
インスタンスを返します。 このデータベースのコレクションにデータを挿入すると、サーバーはその時点でデータベースとコレクションを作成します。
次の例では、クライアントを作成し、 GetDatabase()
メソッドを使用してtest_db
というデータベースにアクセスします。
var client = new MongoClient("<connection string>"); var myDB = mongoClient.GetDatabase("test_db");
データベースを一覧表示する
To see a list of your deployment's databases, call the asynchronous ListDatabaseNamesAsync() method or synchronous ListDatabaseNames() method on your IMongoClient
instance.
To see detailed information about each database, call the asynchronous ListDatabasesAsync() method or synchronous ListDatabases() method on your IMongoClient
instance. These methods return fields describing the databases in the cluster, such as their sizes and whether they contain data.
次のコードは、非同期ListDatabaseNamesAsync()
メソッドまたは同期ListDatabaseNames()
メソッドを使用して、クラスター内のデータベースの名前を一覧表示する方法を示しています。
await mongoClient.ListDatabaseNamesAsync();
mongoClient.ListDatabaseNames();
データベースの削除
Dropping a database permanently deletes all the data in that database's collections. To drop a database, call the asynchronous DropDatabaseAsync() method or synchronous DropDatabase() method on your IMongoClient
instance, passing the database name as the parameter.
次のコードは、非同期DropDatabaseAsync()
メソッドまたは同期DropDatabase()
メソッドを使用して、 test_db
というデータベースを削除する方法を示しています。
await mongoClient.DropDatabaseAsync("test_db");
mongoClient.DropDatabase("test_db");
警告
データベースの削除によるデータの削除
データベースを永続的に削除すると、データベースのコレクション内のすべてのドキュメントと、それらのコレクションのすべてのインデックスが永続的に削除されます。 データベースを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
コレクションにアクセスする
IMongoCollectionインスタンスをデータベースから取得することで、コレクションにアクセスできます。 IMongoCollection
インスタンスを使用して、データ操作を実行し、集計を作成し、インデックスを管理できます。IMongoCollection
を検索するには、 インスタンスで GetCollection() IMongoDatabase
メソッドを呼び出します。また、任意の MongoCollectionSettings をパラメーターとして渡して、コレクションへのアクセス方法をカスタマイズできます。
存在しないコレクションの名前をこのメソッドに渡すと、ドライバーは引き続きIMongoCollection
インスタンスを返します。 このコレクションにデータを挿入すると、サーバーによってコレクションが作成されます。 コレクションを明示的に作成する方法については、このガイドの「 コレクションの作成 」セクションを参照してください。
この例では、 GetCollection()
メソッドを使用して、 myDB
変数が参照するデータベースからcoll_xyz
というコレクションにアクセスします。
var myColl = myDB.GetCollection<BsonDocument>("coll_xyz");
コレクション パラメータ化
コレクションのデータをシリアル化するデータ型を指定して、 IMongoCollection
インスタンスをパラメータ化する必要があります。 特定のタイプでパラメータ化されているIMongoCollection
インスタンスでメソッドを呼び出すと、メソッドはこのタイプのインスタンスを受け入れるか返します。
次の例は、 BsonDocument
型のコレクションをパラメータ化する方法を示しています。
var collection = database.GetCollection<BsonDocument>("coll_xyz", settings);
Tip
BsonDocument
型ではなく、データをモデル化するカスタムタイプでIMongoCollection
インスタンスをパラメータ化することをおすすめします。 特定のデータをモデル化する型を定義することで、繰り返しの直列化と検証を回避できます。
.NET/C# ドライバーで直列化の詳細については、 直列化に関するガイドを参照してください。
コレクションを作成する
You can explicitly create a collection by calling the asynchronous CreateCollectionAsync() method or synchronous CreateCollection() method on your IMongoDatabase
instance.
このメソッドは、コレクション名と任意の CreateCollectionOptions 型をパラメーターとして受け取ります。その後は作成されたコレクションにアクセスして、データ操作を実行し、集計を作成し、インデックスを管理できます。
次のコードは、非同期CreateCollectionAsync()
メソッドまたは同期CreateCollection()
メソッドを使用して、 myDB
変数が参照するデータベース内にcoll_abc
というコレクションを作成する方法を示しています。
await myDB.CreateCollectionAsync("coll_abc");
myDB.CreateCollection("coll_abc");
listCollections
To see a list of collections in a database, call the asynchronous ListCollectionNamesAsync() method or synchronous ListCollectionNames() method on your IMongoDatabase
instance.
To see detailed information about each database, call the asynchronous ListCollectionsAsync() method or synchronous ListCollections() method on your IMongoDatabase
instance. These methods return fields describing the collections in the database, such as their types and settings.
次のコードは、非同期ListCollectionNamesAsync()
メソッドまたは同期ListCollectionNames()
メソッドを使用して、データベース内のコレクションの名前を一覧表示する方法を示しています。
await myDB.ListCollectionNamesAsync();
myDB.ListCollectionNames();
コレクションの削除
Dropping a collection permanently deletes all the data in that collection. To drop a collection, call the asynchronous DropCollectionAsync() method or the synchronous DropCollection() method on your IMongoCollection
instance.
次のコードは、非同期DropCollectionAsync()
メソッドまたは同期DropCollection()
メソッドを使用して、 coll_abc
というデータベースを削除する方法を示しています。
await myDB.DropCollectionAsync("coll_abc");
myDB.DropCollection("coll_abc");
警告
コレクションを削除するとデータが削除されます
データベースからコレクションを削除すると、そのコレクション内のすべてのドキュメントとそのコレクションのすべてのインデックスが永続的に削除されます。 コレクションを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
詳細情報
このガイドの概念の詳細については、次のドキュメントを参照してください。
ドキュメントの挿入ガイド
「データベースとコレクション」のサーバー マニュアル
サーバー マニュアルのドキュメント