Docs Menu
Docs Home
/ / /
C#/.NET
/

Databases and Collections

On this page

  • Overview
  • Access a Database
  • List Databases
  • Drop a Database
  • Access a Collection
  • Collection Parameterization
  • Create a Collection
  • List Collections
  • Drop a Collection
  • Additional Information

In this guide, you can learn how to use the .NET/C# Driver to access and manage MongoDB databases and collections.

MongoDB organizes data in a hierarchical structure. A MongoDB deployment contains one or more databases, and each database contains one or more collections. In each collection, MongoDB stores data as documents that contain field-and-value pairs.

To learn more about the document data format, see Documents in the Server manual.

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.

To create an IMongoDatabase, call the GetDatabase() method on an IMongoClient instance, passing the database name as a parameter. You can also pass an optional MongoDatabaseSettings as a parameter to customize how you access a database.

If you pass the name of a nonexistent database to the GetDatabase() method, the driver still returns an IMongoDatabase instance. When you insert any data into a collection in this database, the server creates the database and collection at that time.

The following example creates a client, then uses the GetDatabase() method to access a database called 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.

The following code shows how to use the asynchronous ListDatabaseNamesAsync() method or the synchronous ListDatabaseNames() method to list the names of the databases in a cluster:

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.

The following code shows how to use the asynchronous DropDatabaseAsync() method or the synchronous DropDatabase() method to drop a database called test_db:

await mongoClient.DropDatabaseAsync("test_db");
mongoClient.DropDatabase("test_db");

Warning

Dropping a Database Deletes Data

Dropping a database permanently deletes all documents in the database's collections and all indexes on those collections. After you drop a database, you cannot access or restore any of its data.

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.

If you pass the name of a nonexistent collection to this method, the driver still returns an IMongoCollection instance. When you insert any data into this collection, the server creates it. To learn how to explicitly create a collection, see the Create a Collection section of this guide.

This example uses the GetCollection() method to access a collection called coll_xyz from a database referenced by the myDB variable:

var myColl = myDB.GetCollection<BsonDocument>("coll_xyz");

You must parameterize your IMongoCollection instance by specifying what data type you want to serialize the collection's data into. When you call a method on a IMongoCollection instance that is parameterized with a specific type, the method accepts or returns instances of this type.

The following example shows how to parameterize a collection with the BsonDocument type:

var collection = database.GetCollection<BsonDocument>("coll_xyz", settings);

Tip

We recommend that you parameterize your IMongoCollection instance with a custom type that models your data instead of the BsonDocument type. You can avoid repetitive serialization and validation by defining a type that models your specific data.

To learn more about serialization in the .NET/C# Driver, see the guide on Serialization.

You can explicitly create a collection by calling the asynchronous CreateCollectionAsync() method or synchronous CreateCollection() method on your IMongoDatabase instance.

This method takes the collection name and an optional CreateCollectionOptions type as parameters. You can then access the created collection to perform data operations, create aggregations, and manage indexes.

The following code shows how to use the asynchronous CreateCollectionAsync() method or the synchronous CreateCollection() method to create a collection called coll_abc within a database referenced by the myDB variable:

await myDB.CreateCollectionAsync("coll_abc");
myDB.CreateCollection("coll_abc");

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.

The following code shows how to use the asynchronous ListCollectionNamesAsync() method or the synchronous ListCollectionNames() method to list the names of the collections in a database:

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.

The following code shows how to use the asynchronous DropCollectionAsync() method or the synchronous DropCollection() method to drop a database called coll_abc:

await myDB.DropCollectionAsync("coll_abc");
myDB.DropCollection("coll_abc");

Warning

Dropping a Collection Deletes Data

Dropping a collection from your database permanently deletes all documents within that collection and all indexes on that collection. After you drop a collection, you cannot access or restore any of its data.

For more information about the concepts in this guide, see the following documentation:

Back

Network Compression