26

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?

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Kar
  • 485
  • 1
  • 4
  • 9

6 Answers6

63

EDIT:

They decided to add back synchronous support (although async is still preferable for IO operations) so you can simply use:

var list = collection.Find(_ => true).ToList();

Original:

Don't block synchronously on asynchronous code. It's bad for performance and could lead to deadlocks.

If you want to keep your application synchronous it's recommended that you keep using the old synchronous driver.

In the new v2.0 driver the async option should look like this:

async Task FooAsync()
{
    var list = await collection.Find(_ => true).ToListAsync();
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • Thanks for the info. However if I return a task wouldn't that propogate to the calling methods as well. I mean, I would have to make all my methods asyc right? – Kar Apr 20 '15 at 15:52
  • 1
    @Kar yes, you would. That's a common result of using `async-await`. It should rise up as high as possible. Otherwise I would suggest either using the old API (which is still blocking on `async` requests) or using the latest v1.9 version which is completely synchronous. – i3arnon Apr 20 '15 at 18:55
  • 2
    "*could lead to deadlocks*" It shouldn't as MongoDB 2.0 driver uses await by calling `ConfigureAwait(false)` on `Task`s. – tugberk Jun 17 '15 at 06:33
  • 1
    @tugberk It was a general warning. But even for the MongoDB driver, you shouldn't rely on other people's code being bug-free (or your own). That makes your code brittle. – i3arnon Jun 17 '15 at 10:26
  • I agree but just wanted to highlight the fact that MongoDB driver does the right thing to prevent you from having a deadlock on cases where you have no other chance but to block (e.g. inside an [ASP.NET MVC child action](https://github.com/tugberkugurlu/Bloggy/blob/08feca907038f4bd39ad63bf7164b218c2c05743/src/Bloggy.Client.Web/Controllers/ArchiveController.cs#L51-L62). As you can see, I have a helper to prevent me from having the deadlock as [RavenDB client had a bug on that](https://github.com/ravendb/ravendb/commit/bd7001c47c6844519f7a409fa1030e211f4d4d59)). – tugberk Jun 17 '15 at 11:35
  • How would I use the above in context. – MiloTheGreat Oct 13 '15 at 06:18
  • If you don't want to use async, this works also: `var list = collection.Find(_ => true).ToList();` – Eric P Sep 16 '16 at 09:50
2

With the MongoDb version 2.2.4, the implementation changed a little bit. Following the best practices let's build the MongoDb connection like this:

public static class PatientDb
{
    public static IMongoCollection<Patient> Open()
    {
        var client = new MongoClient("mongodb://localhost");
        var db = client.GetDatabase("PatientDb");
        return db.GetCollection<Patient>("Patients");
    } 
}

Now is returned a interface of IMongoCollection instead of instance of a concrete class like MongoCollection. There is no need of create a instance of server to get the database anymore, the client can reach the database directly.

Then in the controller is done like this:

public class PatientController : ApiController
{
    private readonly IMongoCollection<Patient> _patients;

    public PatientController()
    {
        _patients = PatientDb.Open();
    }
    public IEnumerable<Patient> Get()
    {
        return _patients.Find(new BsonDocument()).ToEnumerable();
    }
}

Where _patients is a IMongoCollection and to retrieve all Patients instead to use the FindAll() now is used the Find() where the filter is an new instance of BsonDocument.

Pedro S Cord
  • 1,301
  • 17
  • 20
2

To retrieve all, you can use an empty filter as per the documentation

FilterDefinition<T>.Empty

For example

    public async Task<IEnumerable<ClassA>> GetAllAsync() =>
                await database.GetCollection<ClassA>(Collection.MsgContentColName)
               .Find(FilterDefinition<ClassA>.Empty).ToListAsync();
Eli
  • 414
  • 5
  • 10
1

This is with MongoDb C# Driver 2.2

The new C# driver is asynchronous. Like it or not it should be dealt with. It will come into handy in the future. But for now...

In the code below the asynchronous call is effectively made synchronous because of the code "result.GetAwaiter().GetResult();". This makes the asynchronous code execute to finality in the normal flow.

    static void MongoGoNow()
    {
        IMongoCollection<ClassA> collection = db.GetCollection<ClassA>(Collection.MsgContentColName);
        var result = TestFind(collection);
        result.GetAwaiter().GetResult();
        //What's next???
    }

    static async Task TestFind(IMongoCollection<ClassA> MyCollection)
    {
        var filter = new BsonDocument();
        var count = 0;
        using (var cursor = await MyCollection.FindAsync(filter))
        {
            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                foreach (var document in batch)
                {
                    // process document
                    count++;
                }
            }
        }       

You could also merge the last two lines of code in the Main method as follows:

    static void MongoGoNow()
    {
        IMongoCollection<ClassA> collection = db.GetCollection<ClassA>(Collection.MsgContentColName);
        TestFind(collection).GetAwaiter().GetResult();
        //What's next???
    }
CodeBon
  • 1,134
  • 8
  • 9
1

This would be a equivalent to FindAll(),

var list = await collection.Find(new BsonDocument()).ToListAsync();
Femil Shajin
  • 1,768
  • 6
  • 24
  • 38
-18

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;
shA.t
  • 16,580
  • 5
  • 54
  • 111
XtremeBytes
  • 1,469
  • 8
  • 12
  • 2
    What is filter? That's not defined or explained anywhere. This does not address the question and contains unexplained/undefined code. The poster wants a FindAll. – Tim Hardy May 14 '15 at 18:22
  • 2
    Filter is your FilterDefinition and if you dont have one just use "new BsonDocument()". OP didn't ask for that clarification so thought he figured it out. – XtremeBytes May 15 '15 at 14:52
  • 1
    Set filter to "{}", that will return all, although not the best. – MiloTheGreat Oct 13 '15 at 06:17