|
| 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 | +``` |
0 commit comments