如何在 Windows 上安装 MongoDB
MongoDB C# Driver 快速入门指南
这是 MongoDB 驱动快速入门的第二部分,我们可以看到一些管理层面的方法。在第一部分中,我们介绍了如何执行一些简单的CRUD操作。
![](https://img.haomeiwen.com/i1366611/698a27df28727b62.jpeg)
Setup
下面的例子,介绍了如何快速创建和链接client
database
和 collection
变量。
var client = new MongoClient();
var database = client.GetDatabase("foo");
var collection = client.GetCollection<BsonDocument>("bar");
标注:
在
client
中调用GetDatabase
方法不会创建相应的数据库,相同的,在database
中调用GetCollection
方法也不会创建相应的集合。只有当数据被写入成功的时候,相应的database
或collection
才会被创建。比如创建索引或将文档插入之前不存在的集合中。
List the Databases
你可以使用 ListDatabases
或者 ListDatabasesAsync
方法获取到所有数据的集合。
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
方法删除相应的数据库。
client.DropDatabase("foo");
await client.DropDatabaseAsync("foo");
Create a Collection
一个数据库中的集合会被自动创建,直到该集合首次插入一个文档。使用 CreateCollection
或者 CreateCollectionAsync
方法,你可以创建一个指定自定义大小的盖子集合。例如,创建一个1MB大小的盖子集合:
var options = new CreateCollectionOptions { Capped = true, MaxSize = 1024 * 1024 };
database.CreateCollection("cappedBar", options);
await database.CreateCollectionAsync("cappedBar", options);
Drop a Collection
你可以使用 DropCollection
或者 DropCollectionAsync
方法去删除一个集合:
database.DropCollection("cappedBar");
await database.DropCollectionAsync("cappedBar");
Create an Index
MongoDB数据库支持二级索引. 你可以对指定一个字段创建索引,也可以对多个字段结合创建复合索引,并为每个字段指定一个排序方式。1 表示升序,-1 表示降序。下面的例子将为字段 i 创建了一个升序的索引:
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);
更多关于索引字段定义构造器参见 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()));
}
上面的例子将会打印下面的信息:
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.test" }
{ "v" : 1, "key" : { "i" : 1 }, "name" : "i_1", "ns" : "mydb.test" }
Text Indexes
MongoDB 数据库同样支持文本搜索索引。文本索引可以在字段值类型为字符串的字段或者字符串类型数组上创建。下面举例在集合中为“text”字段创建文本索引:
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中,文本索引现在已经集成到主查询语言中,并且默认启用:
// 插入一些文档
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);
以上将会打印出:
Text search matches: 2
Text search matches (english): 2
Highest scoring document: { "_id" : 1, "content" : "additional content", "score" : 0.75 }
想要获取更多关于文本查询的信息,参见 文本索引 和 文本查询操作 。
Running a Command
不是所有的命令都有明确的帮助器,不过,你可以通过 RunCommand
或者 RunCommandAsync
来执行所有的命令。例如我们可以通过如下的方式:
var buildInfoCommand = new BsonDocument("buildinfo", 1);
var result = database.RunCommand(buildInfoCommand);
var result = await database.RunCommandAsync(buildInfoCommand);
网友评论