我遇到一个问题,即 C# 驱动程序未使用 async-await 或 synchronous 方法返回任何数据。
当尝试在命令行中运行时,它完美地工作,这是片段:
db.Collection_StudentResults.aggregate([ { $unwind: "$modules" }, { $match: { "studentNumber": "", "modules.code": "" } } ])
下面是我在 C# 中的设置方式:
public static async Task<BsonDocument> getSingleStudentData(string studentNumber)
{
var client = new MongoClient("mongodb://localhost:27017");
var db = client.GetDatabase("dbStudents");
var collection = db.GetCollection<BsonDocument>("Collection_StudentResults");
var aggregate = collection.Aggregate()
.Unwind("modules")
.Match(new BsonDocument { { "studentNumber", studentNumber } });
var result = await aggregate.ToListAsync();
return result.FirstOrDefault();
}
使用的驱动程序:v2.4.0 MongoDB 版本:v3.2.10
在Collection_StudentResults中,第一个文档包含studentNumber和modules数组,在modules数组中每个文档有代码字段。
求助! 谢谢
最佳答案
对不起 - 我的坏,坏坏坏...
我在我的构建器脚本中错过了 db = db.getSiblingDB - 这导致数据进入根数据库。
祝一切顺利
关于c# - mongoDB C# 驱动程序未返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41027960/