Adminstration

The administration operations exist in multiple places in the driver’s API. Database-related operations exist on the database object and collection-related operations exist on the collection object. If there isn’t a method for the admin operation you want to use, the RunCommandAsync method on IMongoDatabase is available.

Databases

These operations exist on the IMongoClient interface.

Getting a database

To get a database, use the GetDatabase.

Note

There is no command for creating a database. The database will be created the first time it is used.

// get the test database
var db = client.GetDatabase("test");

Dropping a database

Use the DropDatabaseAsync method.

// drops the test database
await client.DropDatabaseAsync("test");

Listing the databases

Use the ListDatabasesAsync method.

using (var cursor = await client.ListDatabaseAsync())
{
    var list = await cursor.ToListAsync();
    // do something with the list
}

Collections

These operations exists on the IMongoDatabase interface.

Getting a collection

The GetCollection<TDocument> method returns an IMongoCollection<TDocument>.

The generic parameter on the method defines the schema your application will use when working with the collection. Generally, this type will either be a BsonDocument which provides no schema enforcement or a mapped class (POCO).

// gets a collection named "foo" using a BsonDocument
var collection = db.GetCollection<BsonDocument>("foo");

For more information on working with collections, see the CRUD Operations section.

Creating a collection

Just like databases, there is no need to create a collection before working with it. It will be created upon first use. However, certain features of collections require explicit creation. The CreateCollectionAsync method allows you to specify not only a name, but also CreateCollectionOptions.

// creates a capped collection named "foo" with a maximum size of 10,000 bytes
await db.CreateCollectionAsync(
    "foo", 
    new CreateCollectionOptions
    {
        Capped = true,
        MaxSize = 10000
    });

Dropping a collection

Use the DropCollectionAsync method.

// drops the "foo" collection
await db.DropCollectionAsync("test");

Listing the collections

Use the ListCollectionsAsync method.

using (var cursor = await db.ListCollectionsAsync())
{
    var list = await cursor.ToListAsync();
    // do something with the list
}

Renaming a collection

Use the RenameCollectionAsync method.

// rename the "foo" collection to "bar"
await db.RenameCollectionAsync("foo", "bar");

Indexes

IMongoCollection<T> contains an Indexes property which gives access to all the index-related operations for a collection.

A number of the methods take an IndexKeysDefinition<TDocument>. See the documentation on the index keys builder for more information.

Creating an index

Use the CreateOneAsync to create a single index. For instance, to create an ascending index on the “x” and “y” fields,

await collection.Indexes.CreateOneAsync("{x: 1, y: 1}");

// or

await collection.Indexes.CreateOneAsync(new BsonDocument("x", 1).Add("y", 1));

// or

await collection.Indexes.CreateOneAsync(Builders<BsonDocument>.IndexKeys.Ascending("x").Ascending("y"));

In addition, there are a number of options available when creating index. These are present on the optional CreateIndexOptions parameter. For instance, to create a unique ascending index on “x”:

await collection.Indexes.CreateOneAsync("{x: 1}", new CreateIndexOptions { Unique = true });

Dropping an index

Use the DropOneAsync to drop a single index or the DropAllAsync to drop all indexes.

// drop the index named "x_1";
await collection.Indexes.DropOneAsync("x_1");

// drop all indexes
await collection.Indexes.DropAllAsync();

Listing indexes

To see all the indexes in a collection, use the ListAsync method.

using(var cursor = await collection.Indexes.ListAsync())
{
    var list = await cursor.ToListAsync();
    // do something with the list...
}