我试图找到一种方法来制作一个方法,用最新的驱动程序从mongodb中检索数据。我找到的所有指南都是旧的驱动程序版本,或者我无法使其工作。我尝试使用mongodb网站上的教程制作它,但它只是打印结果,我需要创建一个方法,返回包含我的数据的列表。谁能告诉我一个有效的方法?
public async Task<List<BsonDocument>> FooAsync()
{
var Client = new MongoClient();
var DB = Client.GetDatabase("DB");
var collection = DB.GetCollection<BsonDocument>("Users");
var a = await collection.Find(new BsonDocument()).ToListAsnyc();
return (a);
}
public static List<BsonDocument> aba()
{
var task = FooAsync();
var result = task.Result;
return (result);
}
答案 0 :(得分:0)
我正在使用最新的MongoDB API添加方法。
public static async Task Single()
{
var _client = new MongoClient(CONNECTION_STRING);
var _database = _client.GetDatabase(DATABASE_NAME);
var _collection = _database.GetCollection<BsonDocument>(COLLECTION_NAME);
var filterBuilder = Builders<BsonDocument>.Filter;
var filter = filterBuilder.Gt("name", "gt")
var projectBuilder = Builders<BsonDocument>.Projection;
var projection = projectBuilder.Include("name").Include("lastname").Include("age").Exclude("_id");
var count = 0;
var results = await _collection.Find(filter).Limit
(500).Project(projection).ToListAsync();
foreach(var result in results)
{
Console.WriteLine(result);
count++;
}
Console.WriteLine("total count : " + count);
}