简体   繁体   English

mongoDB C#驱动程序未返回任何数据

[英]mongoDB C# Driver is not returning any data

I am having a problem where C# Driver is not returning any data with either using async-await or synchronous method. 我遇到一个问题,其中C#驱动程序不使用异步等待同步方法返回任何数据。

When trying to run in the command line, it works perfectly, here's the snippet: 尝试在命令行中运行时,它可以完美运行,以下是代码段:

db.Collection_StudentResults.aggregate([ { $unwind: "$modules" }, { $match: { "studentNumber": "", "modules.code": "" } } ])

and here's how I have it setup in C#: 这就是我在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();
    }

Drivers Used: v2.4.0 MongoDB Version: v3.2.10 使用的驱动程序: v2.4.0 MongoDB版本: v3.2.10

In Collection_StudentResults , the first document contains the studentNumber and modules array, in the modules array each document has code field. Collection_StudentResults中 ,第一个文档包含studentNumbermodules数组,在modules数组中,每个文档都有代码字段。

Please help! 请帮忙! Thanks 谢谢

Sorry - my bad, bad bad bad... 抱歉-我的糟糕,糟糕的糟糕...

I missed the db = db.getSiblingDB in my builder script - which caused the data to go into the root database. 我错过了构建器脚本中的db = db.getSiblingDB-这导致数据进入根数据库。

All the best. 祝一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM