MongoDB Driver快速浏览

以下内容来自:

https://www.cnblogs.com/axel10/p/8459996.html

建立连接

以下示例显示了连接到本地计算机上的一个或多个服务器的三种方法。

// To directly connect to a single MongoDB server
// (this will not auto-discover the primary even if it's a member of a replica set)
var client = new MongoClient();

// or use a connection string
var client = new MongoClient("mongodb://localhost:27017");

// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
var client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019");

获取数据库

要获取数据库,请在上面的GetDatabase方法中指定数据库的名称client。如果数据库尚不存在,那也没问题。它将在第一次使用时创建。

var database = client.GetDatabase("foo");

插入多个文档

要插入多个文档,可以使用InsertManyor InsertManyAsync方法。

// generate 100 documents with a counter ranging from 0 - 99
var documents = Enumerable.Range(0, 100).Select(i => new BsonDocument("counter", i));
collection.InsertMany(documents);
await collection.InsertManyAsync(documents);

计数文件

现在我们已经插入了101个文档(我们在循环中加入了100个,加上第一个文档),我们可以检查是否全部使用Countor CountAsync方法。以下代码应该将count的值设置为101。

var count = collection.Count(new BsonDocument());
var count = await collection.CountAsync(new BsonDocument());

查找集合中的第一个文档

var document = collection.Find(new BsonDocument()).FirstOrDefault();
Console.WriteLine(document.ToString());
var document = await collection.Find(new BsonDocument()).FirstOrDefaultAsync();
Console.WriteLine(document.ToString());

查找集合中的所有文档

var documents = collection.Find(new BsonDocument()).ToList();
var documents = await collection.Find(new BsonDocument()).ToListAsync();

使用过滤器获取单个文档

我们可以创建一个过滤器来传递给Find方法,以获取我们的集合中的文档的子集。例如,如果我们想要查找“i”字段的值为71的文档,我们将执行以下操作:

var filter = Builders<BsonDocument>.Filter.Eq("i", 71);
var document = collection.Find(filter).First();
Console.WriteLine(document);
var document = await collection.Find(filter).FirstAsync();
Console.WriteLine(document);
它应该只打印一个文件:

{ "_id" : ObjectId("5515836e58c7b4fbc756320b"), "i" : 71 }

排序文件

我们通过调用Sort方法为查询查询添加一个排序。下面我们使用Exists过滤器构建器方法和Descending排序构建器方法对我们的文档进行排序:

var filter = Builders<BsonDocument>.Filter.Exists("i");
var sort = Builders<BsonDocument>.Sort.Descending("i");
var document = collection.Find(filter).Sort(sort).First();
var document = await collection.Find(filter).Sort(sort).FirstAsync();

投影领域

很多时候,我们不需要文档中包含的所有数据。“ 投影”构建器将帮助为查找操作构建投影参数。下面我们将排除“_id”字段并输出第一个匹配文档:

var projection = Builders<BsonDocument>.Projection.Exclude("_id");
var document = collection.Find(new BsonDocument()).Project(projection).First();
Console.WriteLine(document.ToString());
var document = await collection.Find(new BsonDocument()).Project(projection).FirstAsync();
Console.WriteLine(document.ToString());

更新文件

MongoDB支持许多更新运算符

要最多更新1个文档(如果没有匹配过滤器,则可能为0),请使用UpdateOneor UpdateOneAsync方法指定过滤器和更新文档。在这里,我们更新符合过滤器的第一个文档i == 10并将值设置i110

var filter = Builders<BsonDocument>.Filter.Eq("i", 10);
var update = Builders<BsonDocument>.Update.Set("i", 110);
collection.UpdateOne(filter, update);
await collection.UpdateOneAsync(filter, update);

 

posted @ 2019-08-15 14:58  hahaMan  阅读(296)  评论(0编辑  收藏  举报