Skip to content

Commit 04db27c

Browse files
committed
rewriting the getting started documentation to be more thorough.
Conflicts: Docs/reference/content/getting_started/connecting.md Docs/reference/content/getting_started/reading_and_writing.md
1 parent 7822e10 commit 04db27c

File tree

7 files changed

+592
-171
lines changed

7 files changed

+592
-171
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
+++
2+
date = "2015-03-17T15:36:56Z"
3+
draft = false
4+
title = "Admin Quick Tour"
5+
[menu.main]
6+
parent = "Getting Started"
7+
weight = 30
8+
identifier = "Admin Quick Tour"
9+
pre = "<i class='fa'></i>"
10+
+++
11+
12+
## MongoDB Driver Admin Quick Tour
13+
14+
This is the second part of the MongoDB driver quick tour. In this part, we'll look at performing some adminstrative functions. In the [first part]({{< relref "getting_started\quick_tour.md" >}}), we looked at how to perform basic CRUD (create, read, update, delete) operations.
15+
16+
{{% note %}}See the [installation guide]({{< relref "getting_started\installation.md" >}}) for instructions on how to install the MongoDB Driver.{{% /note %}}
17+
18+
19+
## Setup
20+
21+
To get started we’ll quickly connect and create `client`, `database`, and `collection` variables for use in the examples below:
22+
23+
```csharp
24+
var client = new MongoClient();
25+
var database = client.GetDatabase("foo");
26+
var collection = client.GetCollection<BsonDocument>("bar");
27+
```
28+
29+
{{% note %}}Calling the [`GetDatabase`]({{< apiref "M_MongoDB_Driver_MongoClient_GetDatabase" >}}) method on `client` does not create a database. Likewise, calling the [`GetCollection<BsonDocument>`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_GetCollection__1" >}}) method on `database` will not create a collection. Only when a database or collection are written to will they be created. Examples include the creation of an index or the insertion of a document into a previously non-existent collection.{{% /note %}}
30+
31+
32+
## List the Databases
33+
34+
You can list all the databases using the [`ListDatabasesAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_ListDatabasesAsync" >}}) method.
35+
36+
```csharp
37+
using (var cursor = await client.ListDatabasesAsync())
38+
{
39+
await cursor.ForEachAsync(d => Console.WriteLine(d.ToString()));
40+
}
41+
```
42+
43+
44+
## Drop a Database
45+
46+
You can drop a database using the [`DropDatabaseAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_DropDatabaseAsync" >}}) method.
47+
48+
```csharp
49+
await client.DropDatabaseAsync("foo");
50+
```
51+
52+
53+
## Create a Collection
54+
55+
A collections in MongoDB is created automatically simply by inserting a document into it. Using the [`CreateCollectionAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_CreateCollectionAsync" >}}) method, you can also create a collection explicitly in order to to customize its configuration. For example, to create a capped collection sized to 1 megabyte:
56+
57+
```csharp
58+
var options = new CreateCollectionOptions { Capped = true, MaxSize = 1024 * 1024 };
59+
60+
await database.CreateCollectionAsync("cappedBar", options);
61+
```
62+
63+
64+
## Drop a Collection
65+
66+
You can drop a collection with the [`DropCollectionAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_DropCollectionAsync" >}}) method:
67+
68+
```csharp
69+
await database.DropCollectionAsync("cappedBar");
70+
```
71+
72+
73+
## Create an Index
74+
75+
MongoDB supports secondary indexes. To create an index, you just specify the field or combination of fields, and for each field specify the direction of the index for that field; `1` for ascending and `-1` for descending. The following creates an ascending index on the `i` field:
76+
77+
```csharp
78+
await collection.Indexes.CreateOneAsync(new BsonDocument("i", 1));
79+
80+
// or
81+
82+
var keys = Builders<BsonDocument>.IndexKeys.Ascending("i");
83+
await collection.Indexes.CreateOneAsync(keys);
84+
```
85+
86+
More information about the IndexKeys definition builder is in the [reference section]({{< relref "reference\driver\definitions.md#index-keys" >}}).
87+
88+
89+
## List the Indexes in a Collection
90+
91+
Use the [`ListAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_ListAsync" >}}) method to list the indexes in a collection:
92+
93+
```csharp
94+
using (var cursor = await collection.Indexes.ListAsync())
95+
{
96+
await cursor.ForEachAsync(i => Console.WriteLine(i.ToString()));
97+
}
98+
```
99+
100+
The example should print the following indexes:
101+
102+
```json
103+
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.test" }
104+
{ "v" : 1, "key" : { "i" : 1 }, "name" : "i_1", "ns" : "mydb.test" }
105+
```
106+
107+
108+
### Text Indexes
109+
110+
MongoDB also provides text indexes to support searching of string content. Text indexes can include any field whose value is a string or an array of string elements. To create a text index specify the string literal “text” in the index document:
111+
112+
```csharp
113+
await collection.Indexes.CreateOneAsync(new BsonDocument("content", "text"));
114+
115+
// or
116+
117+
var keys = Builders<BsonDocument>.IndexKeys.Text("content");
118+
await collection.Indexes.CreateOneAsync(keys);
119+
```
120+
121+
As of MongoDB 2.6, text indexes are now integrated into the main query language and enabled by default:
122+
123+
```csharp
124+
// insert some documents
125+
await collection.InsertManyAsync(new []
126+
{
127+
new BsonDocument("_id", 0).Add("content", "textual content"),
128+
new BsonDocument("_id", 1).Add("content", "additional content"),
129+
new BsonDocument("_id", 2).Add("content", "irrelevant content"),
130+
});
131+
132+
// find them using the text index
133+
var filter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant");
134+
var matchCount = await collection.CountAsync(filter);
135+
Console.WriteLine("Text search matches: {0}", matchCount);
136+
137+
// find them using the text index with the $language operator
138+
var englishFilter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant", "english");
139+
var matchCount = await collection.CountAsync(filter);
140+
Console.WriteLine("Text search matches (english): {0}", matchCount);
141+
142+
// find the highest scoring match
143+
var projection = Builders<BsonDocument>.Projection.MetaTextScore("score");
144+
var doc = await collection.Find(filter).Project(projection).FirstAsync();
145+
Console.WriteLine("Highest scoring document: {0}", doc);
146+
```
147+
148+
and it should print:
149+
150+
```text
151+
Text search matches: 2
152+
Text search matches (english): 2
153+
Highest scoring document: { "_id" : 1, "content" : "additional content", "score" : 0.75 }
154+
```
155+
156+
For more information about text search, see the [text index]({{< docsref "core/index-text/" >}}) and the [$text query operator]({{< docsref "reference/operator/query/text/" >}}) documentation.
157+
158+
159+
## Running a Command
160+
161+
Not all commands have a specific helper, however you can run any command by using the [`RunCommandAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_RunCommandAsync__1" >}}) method. Here we call the [buildInfo]({{< docsref "reference/command/buildInfo" >}}) command:
162+
163+
```csharp
164+
var buildInfoCommand = new BsonDocument("buildinfo", 1);
165+
var result = await database.RunCommandAsync(buildInfoCommand);
166+
```

Docs/reference/content/getting_started/connecting.md

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

Docs/reference/content/getting_started/index.md

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,8 @@ title = "Getting Started"
99

1010
## Getting Started
1111

12-
This quick-start privides just enough information to get you started using the .NET driver. Refer to the [reference guide]({{< relref "reference\index.md" >}}) for more complete information.
12+
To help you get started quickly on the new driver, follow:
1313

14-
## System Requirements
15-
16-
.NET 4.5 or later is required to utilize the libraries. It has also been tested with Mono 3.10 on OS X.
17-
18-
### Core CLR
19-
20-
As the Core CLR hasn't shipped yet, we don't yet have support for it. We run compatibility reports using the [.NET Portability Analyzer](https://visualstudiogallery.msdn.microsoft.com/1177943e-cfb7-4822-a8a6-e56c7905292b) to mitigate the need to make public API changes when we are ready to release compatible assemblies.
21-
22-
## Nuget Installation
23-
24-
[Nuget](http://www.nuget.org/) is the simplest way to get the driver. There are 4 packages available on nuget.
25-
26-
- [MongoDB.Driver](http://www.nuget.org/packages/mongodb.driver): The new driver. It is mostly free of any legacy code and should be used for all new projects. More documentation can be found in the [reference guide]({{< relref "reference\driver\index.md" >}}).
27-
- [MongoDB.Driver.Core](http://www.nuget.org/packages/mongodb.driver.core): The core of the driver and a dependency of MongoDB.Driver. You will probably not use this package directly. More documentation can be found in the [reference guide]({{< relref "reference\driver_core\index.md" >}}).
28-
- [MongoDB.Bson](http://www.nuget.org/packages/mongodb.bson): The BSON layer. It is a dependency of MongoDB.Driver.Core. It may be used by itself. More documentation can be found in the [reference guide]({{< relref "reference\bson\index.md" >}}).
29-
- [mongocsharpdriver](http://www.nuget.org/packages/mongocsharpdriver): The compatibility layer for those upgrading from our 1.x series. This should not be used for new projects. More information can be found in the [1.x documentation](http://mongodb.github.io/mongo-csharp-driver/1.x);
30-
31-
## Binary Installation
32-
33-
Alternatively, if you'd like to pull down binaries, you can do that from the [releases section](https://github.com/mongodb/mongo-csharp-driver/releases) on our [github repository](https://github.com/mongodb/mongo-csharp-driver), which contains zip files for each release.
34-
35-
The assembly names mostly correlate strongly with the package names above. For new applications, you'll add references to `MongoDB.Driver.dll`, `MongoDB.Driver.Core.dll`, and `MongoDB.Bson.dll`. For those working with legacy applications, you'll also want to add a reference to `MongoDB.Driver.Legacy.dll`.
14+
- [Installation]({{< relref "getting_started\installation.md" >}})
15+
- [Quick Tour]({{< relref "getting_started\quick_tour.md" >}})
16+
- [Admin Quick Tour]({{< relref "getting_started\admin_quick_tour.md" >}})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
+++
2+
date = "2015-03-17T15:36:56Z"
3+
draft = false
4+
title = "Installation"
5+
[menu.main]
6+
parent = "Getting Started"
7+
weight = 10
8+
identifier = "Installation"
9+
pre = "<i class='fa'></i>"
10+
+++
11+
12+
## System Requirements
13+
14+
.NET 4.5 or later is required to utilize the libraries. It has also been tested with Mono 3.10 on OS X.
15+
16+
### Core CLR
17+
18+
As the Core CLR hasn't shipped yet, we don't yet have support for it. We run compatibility reports using the [.NET Portability Analyzer](https://visualstudiogallery.msdn.microsoft.com/1177943e-cfb7-4822-a8a6-e56c7905292b) to mitigate the need to make public API changes when we are ready to release compatible assemblies.
19+
20+
## Nuget Installation
21+
22+
[Nuget](http://www.nuget.org/) is the simplest way to get the driver. There are 4 packages available on nuget.
23+
24+
- [MongoDB.Driver](http://www.nuget.org/packages/mongodb.driver): The new driver. It is mostly free of any legacy code and should be used for all new projects. More documentation can be found in the [reference guide]({{< relref "reference\driver\index.md" >}}).
25+
- [MongoDB.Driver.Core](http://www.nuget.org/packages/mongodb.driver.core): The core of the driver and a dependency of MongoDB.Driver. You will probably not use this package directly. More documentation can be found in the [reference guide]({{< relref "reference\driver_core\index.md" >}}).
26+
- [MongoDB.Bson](http://www.nuget.org/packages/mongodb.bson): The BSON layer. It is a dependency of MongoDB.Driver.Core. It may be used by itself. More documentation can be found in the [reference guide]({{< relref "reference\bson\index.md" >}}).
27+
- [mongocsharpdriver](http://www.nuget.org/packages/mongocsharpdriver): The compatibility layer for those upgrading from our 1.x series. This should not be used for new projects. More information can be found in the [1.x documentation](http://mongodb.github.io/mongo-csharp-driver/1.x);
28+
29+
## Binary Installation
30+
31+
Alternatively, if you'd like to pull down binaries, you can do that from the [releases section](https://github.com/mongodb/mongo-csharp-driver/releases) on our [github repository](https://github.com/mongodb/mongo-csharp-driver), which contains zip files for each release.
32+
33+
The assembly names mostly correlate strongly with the package names above. For new applications, you'll add references to `MongoDB.Driver.dll`, `MongoDB.Driver.Core.dll`, and `MongoDB.Bson.dll`. For those working with legacy applications, you'll also want to add a reference to `MongoDB.Driver.Legacy.dll`.
34+

0 commit comments

Comments
 (0)