Skip to content

Commit

Permalink
rewriting the getting started documentation to be more thorough.
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed May 15, 2015
1 parent 03c4468 commit 247f526
Show file tree
Hide file tree
Showing 7 changed files with 592 additions and 173 deletions.
166 changes: 166 additions & 0 deletions Docs/reference/content/getting_started/admin_quick_tour.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
+++
date = "2015-03-17T15:36:56Z"
draft = false
title = "Admin Quick Tour"
[menu.main]
parent = "Getting Started"
weight = 30
identifier = "Admin Quick Tour"
pre = "<i class='fa'></i>"
+++

## MongoDB Driver Admin Quick Tour

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.

{{% note %}}See the [installation guide]({{< relref "getting_started\installation.md" >}}) for instructions on how to install the MongoDB Driver.{{% /note %}}


## Setup

To get started we’ll quickly connect and create `client`, `database`, and `collection` variables for use in the examples below:

```csharp
var client = new MongoClient();
var database = client.GetDatabase("foo");
var collection = client.GetCollection<BsonDocument>("bar");
```

{{% 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 %}}


## List the Databases

You can list all the databases using the [`ListDatabasesAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_ListDatabasesAsync" >}}) method.

```csharp
using (var cursor = await client.ListDatabasesAsync())
{
await cursor.ForEachAsync(d => Console.WriteLine(d.ToString()));
}
```


## Drop a Database

You can drop a database using the [`DropDatabaseAsync`]({{< apiref "M_MongoDB_Driver_IMongoClient_DropDatabaseAsync" >}}) method.

```csharp
await client.DropDatabaseAsync("foo");
```


## Create a Collection

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:

```csharp
var options = new CreateCollectionOptions { Capped = true, MaxSize = 1024 * 1024 };

await database.CreateCollectionAsync("cappedBar", options);
```


## Drop a Collection

You can drop a collection with the [`DropCollectionAsync`]({{< apiref "M_MongoDB_Driver_IMongoDatabase_DropCollectionAsync" >}}) method:

```csharp
await database.DropCollectionAsync("cappedBar");
```


## Create an Index

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:

```csharp
await collection.Indexes.CreateOneAsync(new BsonDocument("i", 1));

// or
var keys = Builders<BsonDocument>.IndexKeys.Ascending("i");
await collection.Indexes.CreateOneAsync(keys);
```

More information about the IndexKeys definition builder is in the [reference section]({{< relref "reference\driver\definitions.md#index-keys" >}}).


## List the Indexes in a Collection

Use the [`ListAsync`]({{< apiref "M_MongoDB_Driver_IMongoIndexManager_1_ListAsync" >}}) method to list the indexes in a collection:

```csharp
using (var cursor = await collection.Indexes.ListAsync())
{
await cursor.ForEachAsync(i => Console.WriteLine(i.ToString()));
}
```

The example should print the following indexes:

```json
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.test" }
{ "v" : 1, "key" : { "i" : 1 }, "name" : "i_1", "ns" : "mydb.test" }
```


### Text Indexes

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:

```csharp
await collection.Indexes.CreateOneAsync(new BsonDocument("content", "text"));

// or
var keys = Builders<BsonDocument>.IndexKeys.Text("content");
await collection.Indexes.CreateOneAsync(keys);
```

As of MongoDB 2.6, text indexes are now integrated into the main query language and enabled by default:

```csharp
// insert some documents
await collection.InsertManyAsync(new []
{
new BsonDocument("_id", 0).Add("content", "textual content"),
new BsonDocument("_id", 1).Add("content", "additional content"),
new BsonDocument("_id", 2).Add("content", "irrelevant content"),
});

// find them using the text index
var filter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant");
var matchCount = await collection.CountAsync(filter);
Console.WriteLine("Text search matches: {0}", matchCount);

// find them using the text index with the $language operator
var englishFilter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant", "english");
var matchCount = await collection.CountAsync(filter);
Console.WriteLine("Text search matches (english): {0}", matchCount);

// find the highest scoring match
var projection = Builders<BsonDocument>.Projection.MetaTextScore("score");
var doc = await collection.Find(filter).Project(projection).FirstAsync();
Console.WriteLine("Highest scoring document: {0}", doc);
```

and it should print:

```text
Text search matches: 2
Text search matches (english): 2
Highest scoring document: { "_id" : 1, "content" : "additional content", "score" : 0.75 }
```

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.


## Running a Command

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:

```csharp
var buildInfoCommand = new BsonDocument("buildinfo", 1);
var result = await database.RunCommandAsync(buildInfoCommand);
```
58 changes: 0 additions & 58 deletions Docs/reference/content/getting_started/connecting.md

This file was deleted.

27 changes: 4 additions & 23 deletions Docs/reference/content/getting_started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,8 @@ title = "Getting Started"

## Getting Started

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.
To help you get started quickly on the new driver, follow:

## System Requirements

.NET 4.5 or later is required to utilize the libraries. It has also been tested with Mono 3.10 on OS X.

### Core CLR

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.

## Nuget Installation

[Nuget](http://www.nuget.org/) is the simplest way to get the driver. There are 4 packages available on nuget.

- [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" >}}).
- [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" >}}).
- [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" >}}).
- [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);

## Binary Installation

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.

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`.
- [Installation]({{< relref "getting_started\installation.md" >}})
- [Quick Tour]({{< relref "getting_started\quick_tour.md" >}})
- [Admin Quick Tour]({{< relref "getting_started\admin_quick_tour.md" >}})
34 changes: 34 additions & 0 deletions Docs/reference/content/getting_started/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
+++
date = "2015-03-17T15:36:56Z"
draft = false
title = "Installation"
[menu.main]
parent = "Getting Started"
weight = 10
identifier = "Installation"
pre = "<i class='fa'></i>"
+++

## System Requirements

.NET 4.5 or later is required to utilize the libraries. It has also been tested with Mono 3.10 on OS X.

### Core CLR

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.

## Nuget Installation

[Nuget](http://www.nuget.org/) is the simplest way to get the driver. There are 4 packages available on nuget.

- [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" >}}).
- [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" >}}).
- [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" >}}).
- [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);

## Binary Installation

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.

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`.

Loading

0 comments on commit 247f526

Please sign in to comment.