如何在 Windows 上安裝 MongoDB
MongoDB C# Driver 快速入門指南
這是 MongoDB 驅(qū)動(dòng)快速入門的第二部分,我們可以看到一些管理層面的方法臀脏。在第一部分中,我們介紹了如何執(zhí)行一些簡(jiǎn)單的CRUD操作。
Setup
下面的例子呀闻,介紹了如何快速創(chuàng)建和鏈接client
database
和 collection
變量。
var client = new MongoClient();
var database = client.GetDatabase("foo");
var collection = client.GetCollection<BsonDocument>("bar");
標(biāo)注:
在
client
中調(diào)用GetDatabase
方法不會(huì)創(chuàng)建相應(yīng)的數(shù)據(jù)庫(kù)潜慎,相同的捡多,在database
中調(diào)用GetCollection
方法也不會(huì)創(chuàng)建相應(yīng)的集合。只有當(dāng)數(shù)據(jù)被寫入成功的時(shí)候铐炫,相應(yīng)的database
或collection
才會(huì)被創(chuàng)建垒手。比如創(chuàng)建索引或?qū)⑽臋n插入之前不存在的集合中。
List the Databases
你可以使用 ListDatabases
或者 ListDatabasesAsync
方法獲取到所有數(shù)據(jù)的集合倒信。
using (var cursor = client.ListDatabases())
{
foreach (var document in cursor.ToEnumerable())
{
Console.WriteLine(document.ToString()));
}
}
using (var cursor = await client.ListDatabasesAsync())
{
await cursor.ForEachAsync(document => Console.WriteLine(document.ToString()));
}
Drop a Database
你可以使用 DropDatabase
或者 DropDatabaseAsync
方法刪除相應(yīng)的數(shù)據(jù)庫(kù)淫奔。
client.DropDatabase("foo");
await client.DropDatabaseAsync("foo");
Create a Collection
一個(gè)數(shù)據(jù)庫(kù)中的集合會(huì)被自動(dòng)創(chuàng)建,直到該集合首次插入一個(gè)文檔堤结。使用 CreateCollection
或者 CreateCollectionAsync
方法,你可以創(chuàng)建一個(gè)指定自定義大小的蓋子集合鸭丛。例如竞穷,創(chuàng)建一個(gè)1MB大小的蓋子集合:
var options = new CreateCollectionOptions { Capped = true, MaxSize = 1024 * 1024 };
database.CreateCollection("cappedBar", options);
await database.CreateCollectionAsync("cappedBar", options);
Drop a Collection
你可以使用 DropCollection
或者 DropCollectionAsync
方法去刪除一個(gè)集合:
database.DropCollection("cappedBar");
await database.DropCollectionAsync("cappedBar");
Create an Index
MongoDB數(shù)據(jù)庫(kù)支持二級(jí)索引. 你可以對(duì)指定一個(gè)字段創(chuàng)建索引,也可以對(duì)多個(gè)字段結(jié)合創(chuàng)建復(fù)合索引鳞溉,并為每個(gè)字段指定一個(gè)排序方式瘾带。1 表示升序,-1 表示降序熟菲。下面的例子將為字段 i 創(chuàng)建了一個(gè)升序的索引:
collection.Indexes.CreateOne(new BsonDocument("i", 1));
// or
var keys = Builders<BsonDocument>.IndexKeys.Ascending("i");
collection.Indexes.CreateOne(keys);
await collection.Indexes.CreateOneAsync(new BsonDocument("i", 1));
// or
var keys = Builders<BsonDocument>.IndexKeys.Ascending("i");
await collection.Indexes.CreateOneAsync(keys);
更多關(guān)于索引字段定義構(gòu)造器參見 reference section
List the Indexes in a Collection
使用 List
或者 ListAsync
方法列舉集合中的索引:
using (var cursor = collection.Indexes.List())
{
foreach (var document in cursor.ToEnumerable())
{
Console.WriteLine(document.ToString());
}
}
using (var cursor = await collection.Indexes.ListAsync())
{
await cursor.ForEachAsync(document => Console.WriteLine(document.ToString()));
}
上面的例子將會(huì)打印下面的信息:
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.test" }
{ "v" : 1, "key" : { "i" : 1 }, "name" : "i_1", "ns" : "mydb.test" }
Text Indexes
MongoDB 數(shù)據(jù)庫(kù)同樣支持文本搜索索引看政。文本索引可以在字段值類型為字符串的字段或者字符串類型數(shù)組上創(chuàng)建。下面舉例在集合中為“text”字段創(chuàng)建文本索引:
collection.Indexes.CreateOne(new BsonDocument("content", "text"));
// or
var keys = Builders<BsonDocument>.IndexKeys.Text("content");
collection.Indexes.CreateOne(keys);
await collection.Indexes.CreateOneAsync(new BsonDocument("content", "text"));
// or
var keys = Builders<BsonDocument>.IndexKeys.Text("content");
await collection.Indexes.CreateOneAsync(keys);
在MongoDB 2.6中抄罕,文本索引現(xiàn)在已經(jīng)集成到主查詢語言中允蚣,并且默認(rèn)啟用:
// 插入一些文檔
collection.InsertMany(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"),
});
// 使用文本索引找到它們
var filter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant");
var matchCount = collection.Count(filter);
Console.WriteLine("Text search matches: {0}", matchCount);
// 使用文本索引和$語言操作符找到它們
var englishFilter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant", "english");
var matchCount = collection.Count(filter);
Console.WriteLine("Text search matches (english): {0}", matchCount);
// 找到最高得分的比賽
var projection = Builders<BsonDocument>.Projection.MetaTextScore("score");
var doc = collection.Find(filter).Project(projection).First();
Console.WriteLine("Highest scoring document: {0}", doc);
// 插入一些文檔
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"),
});
// 使用文本索引找到它們
var filter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant");
var matchCount = await collection.CountAsync(filter);
Console.WriteLine("Text search matches: {0}", matchCount);
// 使用文本索引和$語言操作符找到它們
var englishFilter = Builders<BsonDocument>.Filter.Text("textual content -irrelevant", "english");
var matchCount = await collection.CountAsync(filter);
Console.WriteLine("Text search matches (english): {0}", matchCount);
// 找到最高得分的比賽
var projection = Builders<BsonDocument>.Projection.MetaTextScore("score");
var doc = await collection.Find(filter).Project(projection).FirstAsync();
Console.WriteLine("Highest scoring document: {0}", doc);
以上將會(huì)打印出:
Text search matches: 2
Text search matches (english): 2
Highest scoring document: { "_id" : 1, "content" : "additional content", "score" : 0.75 }
想要獲取更多關(guān)于文本查詢的信息,參見 文本索引 和 文本查詢操作 呆贿。
Running a Command
不是所有的命令都有明確的幫助器嚷兔,不過,你可以通過 RunCommand
或者 RunCommandAsync
來執(zhí)行所有的命令做入。例如我們可以通過如下的方式:
var buildInfoCommand = new BsonDocument("buildinfo", 1);
var result = database.RunCommand(buildInfoCommand);
var result = await database.RunCommandAsync(buildInfoCommand);