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.

Collections

Getting a collection

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");

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.