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

MongoDb c# driver 2.0 query styles


Early we used a LINQ provider for MongoDb, but now we have migrated to MongoDb c# driver 2.0 and there are a few things that i don't understand.

When we used a LINQ provider the query was like the following:

  var query = from c in dbCollection.AsQueryable()
                        where c.UserId == userId && c.CampaignId == campaignId
                        select c;

But right now i can implement it in a few ways:

var query =
     Builders<Analytics>.Filter.Eq(a => a.UserId, userId) &
     Builders<Analytics>.Filter.Eq(a => a.CampaignId, campaignId);

and

Builders<Analytics>.Filter.Where(
c => c.created >= Convert.ToDateTime(dateFrom) &&
     c.created <= Convert.ToDateTime(dateTo));

Is there are any differences or advantages in these approaches ?


Solution

  • In the MongoDB tutorial I was doing once, they stated that you can always use LINQ, but you should not force it. This is simply because not everything was implemented in LINQ already and probably is not (or is not even possible) to implement via the LINQ "way". It actually means you can use both way. You can use either the Fluent API or the LINQ where possible.