Search code examples
c#.netmongodbmongodb-.net-drivermongodb-csharp-2.0

How to do findAll in the new mongo C# driver and make it synchronous


I was using official C# driver to do a FindAll and upgraded to the new driver 2.0. FindAll is obsolete and is replaced with Find. I am trying to convert a simple method that returns me a list of Class1. Cant find a realistic example using a POCO in their documentation

var collection = database.GetCollection<ClassA>(Collection.MsgContentColName); return collection.FindAll().ToList();

Can someone please help me convert with 2.0 driver and return a list and not a task?


Solution

  • you could do this to achieve the same using 2.0 driver,

    var collection = database.GetCollection<ClassA>(Collection.MsgContentColName);
    var doc = collection.Find(filter).ToListAsync();
    doc.Wait();
    return doc.Result;