|
| 1 | ++++ |
| 2 | +date = "2015-05-07T15:36:56Z" |
| 3 | +draft = false |
| 4 | +title = "Administration" |
| 5 | +[menu.main] |
| 6 | + parent = "Driver" |
| 7 | + identifier = "Administration" |
| 8 | + weight = 30 |
| 9 | + pre = "<i class='fa'></i>" |
| 10 | ++++ |
| 11 | + |
| 12 | +## Adminstration |
| 13 | + |
| 14 | +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`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_RunCommandAsync__1" >}}) method on [`IMongoDatabase`]({{< apiref "T_MongoDB_Driver_IMongoDatabase" >}}) is available. |
| 15 | + |
| 16 | +## Databases |
| 17 | + |
| 18 | +These operations exist on the [`IMongoClient`]({{< apiref "T_MongoDB_Driver_IMongoClient" >}}) interface. |
| 19 | + |
| 20 | +### Getting a database |
| 21 | + |
| 22 | +To get a database, use the [`GetDatabase`]({{< apiref "M_MongoDB_Driver_IMongoClient_GetDatabase" >}}). |
| 23 | + |
| 24 | +{{% note %}} |
| 25 | +There is no command for creating a database. The database will be created the first time it is used. |
| 26 | +{{% /note %}} |
| 27 | + |
| 28 | +```csharp |
| 29 | +// get the test database |
| 30 | +var db = client.GetDatabase("test"); |
| 31 | +``` |
| 32 | + |
| 33 | +### Dropping a database |
| 34 | + |
| 35 | +Use the [`DropDatabaseAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_DropDatabaseAsync" >}}) method. |
| 36 | + |
| 37 | +```csharp |
| 38 | +// drops the test database |
| 39 | +await client.DropDatabaseAsync("test"); |
| 40 | +``` |
| 41 | + |
| 42 | +### Listing the databases |
| 43 | + |
| 44 | +Use the [`ListDatabasesAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_ListDatabasesAsync" >}}) method. |
| 45 | + |
| 46 | +```csharp |
| 47 | +using (var cursor = await client.ListDatabaseAsync()) |
| 48 | +{ |
| 49 | + var list = await cursor.ToListAsync(); |
| 50 | + // do something with the list |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Collections |
| 55 | + |
| 56 | +These operations exists on the [`IMongoDatabase`]({{< apiref "T_MongoDB_Driver_IMongoDatabase" >}}) interface. |
| 57 | + |
| 58 | +### Getting a collection |
| 59 | + |
| 60 | +The [`GetCollection<TDocument>`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_GetCollection__1" >}}) method returns an [`IMongoCollection<TDocument>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}). |
| 61 | + |
| 62 | +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`]({{< relref "reference\bson\bson_document.md" >}}) which provides no schema enforcement or a [mapped class (POCO)]({{< relref "reference\bson\mapping\index.md" >}}). |
| 63 | + |
| 64 | +```csharp |
| 65 | +// gets a collection named "foo" using a BsonDocument |
| 66 | +var collection = db.GetCollection<BsonDocument>("foo"); |
| 67 | +``` |
| 68 | + |
| 69 | +For more information on working with collections, see the [CRUD Operations section]({{< relref "reference\driver\crud\index.md" >}}). |
| 70 | + |
| 71 | +### Creating a collection |
| 72 | + |
| 73 | +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`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_CreateCollectionAsync" >}}) method allows you to specify not only a name, but also [`CreateCollectionOptions`]({{< apiref "T_MongoDB_Driver_CreateCollectionOptions" >}}). |
| 74 | + |
| 75 | +```csharp |
| 76 | +// creates a capped collection named "foo" with a maximum size of 10,000 bytes |
| 77 | +await db.CreateCollectionAsync( |
| 78 | + "foo", |
| 79 | + new CreateCollectionOptions |
| 80 | + { |
| 81 | + Capped = true, |
| 82 | + MaxSize = 10000 |
| 83 | + }); |
| 84 | +``` |
| 85 | + |
| 86 | +### Dropping a collection |
| 87 | + |
| 88 | +Use the [`DropCollectionAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_DropCollectionAsync" >}}) method. |
| 89 | + |
| 90 | +```csharp |
| 91 | +// drops the "foo" collection |
| 92 | +await db.DropCollectionAsync("test"); |
| 93 | +``` |
| 94 | + |
| 95 | +### Listing the collections |
| 96 | + |
| 97 | +Use the [`ListCollectionsAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_ListCollectionsAsync" >}}) method. |
| 98 | + |
| 99 | +```csharp |
| 100 | +using (var cursor = await db.ListCollectionsAsync()) |
| 101 | +{ |
| 102 | + var list = await cursor.ToListAsync(); |
| 103 | + // do something with the list |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Renaming a collection |
| 108 | + |
| 109 | +Use the [`RenameCollectionAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_RenameCollectionAsync" >}}) method. |
| 110 | + |
| 111 | +```csharp |
| 112 | +// rename the "foo" collection to "bar" |
| 113 | +await db.RenameCollectionAsync("foo", "bar"); |
| 114 | +``` |
| 115 | + |
| 116 | +## Indexes |
| 117 | + |
| 118 | +[`IMongoCollection<T>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}) contains an [`Indexes`]({{< apiref "P_MongoDB_Driver_IMongoCollection_1_Indexes" >}}) property which gives access to all the index-related operations for a collection. |
| 119 | + |
| 120 | +A number of the methods take an [`IndexKeysDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_IndexKeysDefinition_1" >}}). See the documentation on the [index keys builder]({{< relref "reference\driver\definitions.md#index-keys ">}}) for more information. |
| 121 | + |
| 122 | +### Creating an index |
| 123 | + |
| 124 | +Use the [`CreateOneAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_CreateOneAsync" >}}) to create a single index. For instance, to create an ascending index on the "x" and "y" fields, |
| 125 | + |
| 126 | +```csharp |
| 127 | +await collection.Indexes.CreateOneAsync("{x: 1, y: 1}"); |
| 128 | + |
| 129 | +// or |
| 130 | +
|
| 131 | +await collection.Indexes.CreateOneAsync(new BsonDocument("x", 1).Add("y", 1)); |
| 132 | + |
| 133 | +// or |
| 134 | +
|
| 135 | +await collection.Indexes.CreateOneAsync(Builders<BsonDocument>.IndexKeys.Ascending("x").Ascending("y")); |
| 136 | +``` |
| 137 | + |
| 138 | +In addition, there are a number of options available when creating index. These are present on the optional [`CreateIndexOptions`]({{< apiref "T_MongoDB_Driver_CreateIndexOptions" >}}) parameter. For instance, to create a unique ascending index on "x": |
| 139 | + |
| 140 | +```csharp |
| 141 | +await collection.Indexes.CreateOneAsync("{x: 1}", new CreateIndexOptions { Unique = true }); |
| 142 | +``` |
| 143 | + |
| 144 | +### Dropping an index |
| 145 | + |
| 146 | +Use the [`DropOneAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_DropOneAsync" >}}) to drop a single index or the [`DropAllAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_DropAllAsync" >}}) to drop all indexes. |
| 147 | + |
| 148 | +```csharp |
| 149 | +// drop the index named "x_1"; |
| 150 | +await collection.Indexes.DropOneAsync("x_1"); |
| 151 | + |
| 152 | +// drop all indexes |
| 153 | +await collection.Indexes.DropAllAsync(); |
| 154 | +``` |
| 155 | + |
| 156 | +### Listing indexes |
| 157 | + |
| 158 | +To see all the indexes in a collection, use the [`ListAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_ListAsync" >}}) method. |
| 159 | + |
| 160 | +```csharp |
| 161 | +using(var cursor = await collection.Indexes.ListAsync()) |
| 162 | +{ |
| 163 | + var list = await cursor.ToListAsync(); |
| 164 | + // do something with the list... |
| 165 | +} |
| 166 | +``` |
0 commit comments