Skip to content

Commit 60e862c

Browse files
committed
CSHARP-1216: added adminstration and index documentation.
1 parent 361c9ca commit 60e862c

File tree

6 files changed

+175
-534
lines changed

6 files changed

+175
-534
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ draft = false
44
title = "Reading and Writing"
55
[menu.main]
66
parent = "Driver"
7-
weight = 40
7+
weight = 50
88
identifier = "Reference Reading and Writing"
99
pre = "<i class='fa'></i>"
1010
+++

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

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

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ draft = false
44
title = "Definitions and Builders"
55
[menu.main]
66
parent = "Driver"
7-
weight = 40
7+
weight = 20
88
identifier = "Definitions and Builders"
99
pre = "<i class='fa'></i>"
1010
+++
@@ -299,7 +299,7 @@ A projection is also used when performing grouping in the [Aggregation Framework
299299

300300
```csharp
301301
// method definition
302-
BsonDocument Sort(SortDefinition<BsonDocument> sort);
302+
BsonDocument Sort(SortDefinition<BsonDocument> filter);
303303

304304
// invocation
305305
var doc = Sort("{ x: 1 }");
@@ -353,17 +353,17 @@ var sort = builder.Ascending("x").Descending("y");
353353

354354
## Updates
355355

356-
[`UpdateDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinition_1" >}}) defines an update document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
356+
[`UpdateDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_UpdateDefinition_1" >}}) defines how to render a valid update document. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
357357

358358
```csharp
359359
// method definition
360-
BsonDocument Update(UpdateDefinition<BsonDocument> update);
360+
BsonDocument Update(UpdateDefinition<BsonDocument> filter);
361361

362362
// invocation
363363
var doc = Update("{ $set: { x: 1 } }");
364364
doc.ToJson().Should().Be("{ $set: { x: 1 } }");
365365

366-
var doc = Update(new BsonDocument("$set", new BsonDocument("x", 1)));
366+
var doc = Sort(new BsonDocument("$set", new BsonDocument("x", 1)));
367367
doc.ToJson().Should().Be("{ $set: { x: 1 } }");
368368
```
369369

@@ -410,60 +410,3 @@ var update = builder.Set("X", 1).Set("Y", 3).Inc("Z", 1);
410410
411411
var update = builder.Set("x", 1).Set("y", 3).Inc("z", 1);
412412
```
413-
414-
## Index Keys
415-
416-
[`IndexKeysDefinition<TDocument>`]({{< apiref "T_MongoDB_Driver_IndexKeysDefinition_1" >}}) defines the index keys component of an index. It is implicity convertible from both a JSON string as well as a [`BsonDocument`]({{< apiref "T_MongoDB_Bson_BsonDocument" >}}). Given the following method, all the following are valid:
417-
418-
```csharp
419-
// method definition
420-
BsonDocument IndexKeys(IndexKeysDefinition<BsonDocument> keys);
421-
422-
// invocation
423-
var doc = IndexKeys("{ x: 1 }");
424-
doc.ToJson().Should().Be("{ $set: { x: 1 } }");
425-
426-
var doc = IndexKeys(new BsonDocument("x", 1));
427-
doc.ToJson().Should().Be("{ $set: { x: 1 } }");
428-
```
429-
430-
### Index Keys Definition Builder
431-
432-
_See the [tests]({{< srcref "MongoDB.Driver.Tests/IndexKeysDefinitionBuilderTests.cs" >}}) for examples._
433-
434-
The [`IndexKeysDefinitionBuilder<TDocument>`]({{< apiref "T_MongoDB_Driver_IndexKeysDefinitionBuilder_1" >}}) provides a nice API for specifying the keys of an index.
435-
436-
For example, to specify this index, `{ x: 1, y: -1, }`, do the following:
437-
438-
```csharp
439-
var builder = Builders<BsonDocument>.IndexKeys;
440-
var keys = builder.Ascending("x").Descending("y");
441-
```
442-
443-
Given the following class:
444-
445-
```csharp
446-
class Widget
447-
{
448-
[BsonElement("x")]
449-
public int X { get; set; }
450-
451-
[BsonElement("y")]
452-
public int Y { get; set; }
453-
}
454-
```
455-
456-
We can achieve the same result in a typed variant:
457-
458-
```csharp
459-
var builder = Builders<Widget>.IndexKeys;
460-
var keys = builder.Ascending(x => x.X).Descending(x => x.Y);
461-
462-
// or
463-
464-
var keys = builder.Ascending("X").Descending("Y");
465-
466-
// or
467-
468-
var keys = builder.Ascending("x").Descending("y");
469-
```

0 commit comments

Comments
 (0)