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, you can run a command directly using the RunCommand
or RunCommandAsync
methods 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 DropDatabase
or DropDatabaseAsync
methods.
// drops the test database
client.DropDatabase("test");
// drops the test database
await client.DropDatabaseAsync("test");
Listing the databases
Use the ListDatabases
or ListDatabasesAsync
methods.
using (var cursor = client.ListDatabase())
{
var list = cursor.ToList();
// do something with the list
}
using (var cursor = await client.ListDatabaseAsync())
{
var list = await cursor.ToListAsync();
// do something with the list
}
Collections
These operations exist 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 CreateCollection
and CreateCollectionAsync
methods allows you to specify not only a name, but also CreateCollectionOptions
.
var options = new CreateCollectionOptions
{
Capped = true,
MaxSize = 10000
};
// creates a capped collection named "foo" with a maximum size of 10,000 bytes
db.CreateCollection("foo", options);
// creates a capped collection named "foo" with a maximum size of 10,000 bytes
await db.CreateCollectionAsync("foo", options);
Creating a clustered collection
New in MongoDB 5.3. Clustered collections are collections created with a clustered index. Documents in a clustered collection are ordered by the clustered index key value. To create a clustered collection:
var options = new CreateCollectionOptions<Product>
{
ClusteredIndex = new ClusteredIndexOptions<Product>()
};
db.CreateCollection("product", options);
Dropping a collection
Use the DropCollection
or DropCollectionAsync
methods.
// drops the "foo" collection
db.DropCollection("test");
// drops the "foo" collection
await db.DropCollectionAsync("test");
Listing the collections
Use the ListCollections
or ListCollectionsAsync
methods.
using (var cursor = db.ListCollections())
{
var list = cursor.ToList();
// do something with the list
}
using (var cursor = await db.ListCollectionsAsync())
{
var list = await cursor.ToListAsync();
// do something with the list
}
Renaming a collection
Use the RenameCollection
or RenameCollectionAsync
methods.
// rename the "foo" collection to "bar"
db.RenameCollection("foo", "bar");
// 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 CreateOne
or CreateOneAsync
methods to create a single index. For instance, to create an ascending index on the “x” and “y” fields,
collection.Indexes.CreateOne("{ x : 1, y : 1 }");
// or
collection.Indexes.CreateOne(new BsonDocument("x", 1).Add("y", 1));
// or
collection.Indexes.CreateOne(Builders<BsonDocument>.IndexKeys.Ascending("x").Ascending("y"));
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”:
var options = new CreateIndexOptions { Unique = true };
collection.Indexes.CreateOne("{ x : 1 }", options);
await collection.Indexes.CreateOneAsync("{ x : 1 }", options);
Also, there is a generic CreateIndexOptions<TDocument>
which extends the non-generic version and provides a number of additional options available when creating an index. For example, to create a wildcard projection
index on “name” and “description.text”, do the following:
Given the following classes:
public class Description
{
[BsonElement("text")]
public string Text { get; set; }
}
public class Widget
{
public ObjectId Id { get; set; }
[BsonElement("name")]
public int Name { get; set; }
[BsonElement("description")]
public Description Description { get; set; }
}
The wildcard projection value can be implicitly convertible from both a JSON string as well as a BsonDocument
.
var options = new CreateIndexOptions<Widget>();
options.WildcardProjection = "{ 'name' : 1, 'description.text' : 1 }";
//or
options.WildcardProjection = new BsonDocument { { "name", 1 }, { "description.text", 1 } };
We can achieve the same result with using a projection
builder:
options.WildcardProjection = Builders<Widget>.Projection.Include(x => x.Name).Include(x => x.Description.Text);
Dropping an index
To drop a single index use the DropOne
or DropOneAsync
methods.
// drop the index named "x_1";
collection.Indexes.DropOne("x_1");
// drop the index named "x_1";
await collection.Indexes.DropOneAsync("x_1");
To drop all indexes use the DropAll
or DropAllAsync
methods
// drop all indexes
collection.Indexes.DropAll();
// drop all indexes
await collection.Indexes.DropAllAsync();
Listing indexes
To list all the indexes in a collection, use the List
or ListAsync
methods.
using (var cursor = collection.Indexes.List())
{
var list = cursor.ToList();
// do something with the list...
}
using (var cursor = await collection.Indexes.ListAsync())
{
var list = await cursor.ToListAsync();
// do something with the list...
}