Overview
このガイドでは、.NET/C# ドライバーを使用して MongoDB のデータベースとコレクションにアクセスおよび管理する方法を学習できます。
MongoDB では、データは階層構造で整理されています。 MongoDB 配置には 1 つ以上のデータベースが含まれ、各データベースには 1 つ以上のコレクションが含まれます。 各コレクションでは、MongoDB は、フィールドと値のペアを含むドキュメントとしてデータを保存します。
ドキュメント データ形式の詳細については、サーバー マニュアルのドキュメントを参照してください。
データベースへのアクセス
You can access a database by retrieving an IMongoDatabase instance from your IMongoClient instance. You can use the returned IMongoDatabase instance to perform database-level operations and access collections that the database contains.
を作成するには、データベース名をパラメータとして渡して、 IMongoDatabaseIMongoClient インスタンスで 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 synchronous ListDatabaseNames() method or asynchronous ListDatabaseNamesAsync() method on your IMongoClient instance.
To see detailed information about each database, call the synchronous ListDatabases() method or asynchronous ListDatabasesAsync() method on your IMongoClient instance. These methods return fields describing the databases in the cluster, such as their sizes and whether they contain data.
次のコードは、同期 ListDatabaseNames() メソッドまたは非同期 ListDatabaseNamesAsync() メソッドを使用して、クラスター内のデータベースの名前を一覧表示する方法を示しています。
mongoClient.ListDatabaseNames();
await mongoClient.ListDatabaseNamesAsync();
データベースの削除
Dropping a database permanently deletes all the data in that database's collections. To drop a database, call the synchronous DropDatabase() method or asynchronous DropDatabaseAsync() method on your IMongoClient instance, passing the database name as the parameter.
次のコードは、同期 DropDatabase() メソッドまたは非同期 DropDatabaseAsync() メソッドを使用して、test_db というデータベースを削除する方法を示しています。
mongoClient.DropDatabase("test_db");
await mongoClient.DropDatabaseAsync("test_db");
警告
データベースの削除によるデータの削除
データベースを永続的に削除すると、データベースのコレクション内のすべてのドキュメントと、それらのコレクションのすべてのインデックスが永続的に削除されます。 データベースを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
コレクションにアクセスする
You can access a collection by retrieving an IMongoCollection instance from your database. You can use an IMongoCollection instance to perform data operations, create aggregations, and manage indexes. To retrieve an IMongoCollection, call the GetCollection() method on an IMongoDatabase instance. You can also pass an optional MongoCollectionSettings as a parameter to customize how you access a collection.
存在しないコレクションの名前をこのメソッドに渡すと、ドライバーは引き続き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 synchronous CreateCollection() method or asynchronous CreateCollectionAsync() method on your IMongoDatabase instance.
このメソッドは、コレクション名と任意の CreateCollectionOptions 型をパラメーターとして受け取ります。その後は作成されたコレクションにアクセスして、データ操作を実行し、集計を作成し、インデックスを管理できます。
次のコードは、同期 CreateCollection() メソッドまたは非同期 CreateCollectionAsync() メソッドを使用して、myDB 変数が参照するデータベース内に coll_abc というコレクションを作成する方法を示しています。
myDB.CreateCollection("coll_abc");
await myDB.CreateCollectionAsync("coll_abc");
listCollections
To see a list of collections in a database, call the synchronous ListCollectionNames() method or asynchronous ListCollectionNamesAsync() method on your IMongoDatabase instance.
To see detailed information about each database, call the synchronous ListCollections() method or asynchronous ListCollectionsAsync() method on your IMongoDatabase instance. These methods return fields describing the collections in the database, such as their types and settings.
次のコードは、同期 ListCollectionNames() メソッドまたは非同期 ListCollectionNamesAsync() メソッドを使用して、データベース内のコレクションの名前を一覧表示する方法を示しています。
myDB.ListCollectionNames();
await myDB.ListCollectionNamesAsync();
コレクションの削除
Dropping a collection permanently deletes all the data in that collection. To drop a collection, call the synchronous DropCollection() or the asynchronous DropCollectionAsync() method on your IMongoCollection instance.
次のコードは、同期 DropCollection() メソッドまたは非同期 DropCollectionAsync() メソッドを使用して、coll_abc というデータベースを削除する方法を示しています。
myDB.DropCollection("coll_abc");
await myDB.DropCollectionAsync("coll_abc");
警告
コレクションを削除するとデータが削除されます
データベースからコレクションを削除すると、そのコレクション内のすべてのドキュメントとそのコレクションのすべてのインデックスが永続的に削除されます。 コレクションを削除した後は、そのデータにアクセスしたり、復元したりすることはできません。
詳細情報
このガイドの概念の詳細については、次のドキュメントを参照してください。
ドキュメントの挿入ガイド
「データベースとコレクション」のサーバー マニュアル
サーバー マニュアルのドキュメント