简体   繁体   中英

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.

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#:

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

In Collection_StudentResults , the first document contains the studentNumber and modules array, in the modules array each document has code field.

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.

All the best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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