Search code examples
c#asynchronousasync-awaitmongodb-.net-drivermongodb-csharp-2.0

MongoDB .NET driver find all : How to write it better?


I am able to query all the items of a collection using 2 approaches

a)

var findAll = await Context.ItemsCollection.FindAsync(_ => true);
var res = await findAll.ToListAsync();

b)

var res = await.Context.ItemsCollection.Find(_ => true).ToListAsync();

Is there a real difference between them?Which one should I prefer ?


Solution

  • There's no real difference. It will eventually behave the same.

    Find doesn't execute the query, while FindAsync does, but using ToListAsync makes that difference irrelevant.