Skip to content

Commit 8b1130f

Browse files
committed
CSHARP-1216: added adminstration and index documentation.
Conflicts: Docs/reference/content/reference/driver/crud/index.md Docs/reference/content/reference/driver/definitions.md
1 parent a6e16e9 commit 8b1130f

File tree

4 files changed

+176
-8
lines changed

4 files changed

+176
-8
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
```

Docs/reference/content/reference/driver/crud/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
+++
22
date = "2015-03-17T15:36:56Z"
33
draft = false
4-
title = "CRUD Operations"
4+
title = "Reading and Writing"
55
[menu.main]
66
parent = "Driver"
77
weight = 50
8+
identifier = "Reference Reading and Writing"
89
pre = "<i class='fa'></i>"
910
+++
1011

11-
## CRUD Operations
12+
## Reading and Writing
1213

1314
All the create, read, update, and delete (CRUD) operations take a similar form and are defined on the [`IMongoCollection<TDocument>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}) interface. All the required fields take the form of a positional parameter and, if any options exists, they are passed in as an instance of an options class. For example, the following method signature exists:
1415

@@ -34,4 +35,4 @@ Animal
3435
- Dog
3536
```
3637

37-
The collection instance must be an [`IMongoCollection<Animal>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}).
38+
The collection instance must be an [`IMongoCollection<Animal>`]({{< apiref "T_MongoDB_Driver_IMongoCollection_1" >}}).

Docs/reference/content/reference/driver/definitions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ draft = false
44
title = "Definitions and Builders"
55
[menu.main]
66
parent = "Driver"
7-
weight = 40
7+
weight = 20
8+
identifier = "Definitions and Builders"
89
pre = "<i class='fa'></i>"
910
+++
1011

@@ -408,4 +409,4 @@ var update = builder.Set("X", 1).Set("Y", 3).Inc("Z", 1);
408409
// or
409410
410411
var update = builder.Set("x", 1).Set("y", 3).Inc("z", 1);
411-
```
412+
```

Docs/reference/content/reference/driver/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ title = "Driver"
1313
The MongoDB .NET Driver is mostly just a wrapper around [MongoDB.Driver.Core]({{< relref "reference\driver_core\index.md" >}}). It takes the very verbose and low-level core driver and creates a nice high-level API.
1414

1515
- [Connecting]({{< relref "reference\driver\connecting.md" >}})
16-
- [Authentication]({{< relref "reference\driver\authentication.md" >}})
17-
- [SSL]({{< relref "reference\driver\ssl.md" >}})
16+
- [Authentication]({{< relref "reference\driver\authentication.md" >}})
17+
- [SSL]({{< relref "reference\driver\ssl.md" >}})
18+
- [Administration]({{< relref "reference\driver\admin.md" >}})
1819
- [Definitions and Builders]({{< relref "reference\driver\definitions.md" >}})
1920
- [CRUD Operations]({{< relref "reference\driver\crud\index.md" >}})
20-
- Indexes
2121
- Error Handling
2222
- Experimental Features

0 commit comments

Comments
 (0)