CountDocumentsAsync throws exception when using expression

I’ve started seeing 'MongoDB.Driver.Linq.ExpressionNotSupportedException' in MongoDB.Driver.dll appearing in my c# output. I use a lot of linq expressions to evaluate what I’m looking for. similar to the following
Collection.CountDocumentsAsync(e => e.League.Id == leagueId)
Now it appears the only way to get these exceptions to stop is to use a Builder to not show these exceptions like so
await Collection.CountDocumentsAsync(Builders<LeagueEvent>.Filter.Eq(e => e.League.Id, leagueId))
However, I still have no problems using the same or similar expressions when performing a FindAsync or DeleteOneAsync, etc. Is there something wrong with my expression, or does CoundDocumentsAsync have different requirements?

Starting in 2.19.0, we upgraded the default LINQ provider from LINQ2 to LINQ3. LINQ3 is a more modern implementation with a lot more functionality. That said, users have encountered edge cases that are not supported. We encourage you to report any problems with the LINQ3 provider in our JIRA project.

You can switch back to LINQ2 provider though we will be removing it in the upcoming 3.0 driver release:

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V2;
var client = new MongoClient(clientSettings);

I attempted to reproduce the reported issue, but the following snippet works as expected with the 2.22.0 driver.

using System;
using MongoDB.Bson;
using MongoDB.Driver;

var client = new MongoClient();
var db = client.GetDatabase("test");
var coll = db.GetCollection<Player>("players");

var leagueId = 42;
var count = await coll.CountDocumentsAsync(e => e.League.Id == leagueId);
Console.WriteLine($"The count is {count}.");

record Player(ObjectId Id, League League);
record League(int Id);

Please create a CSHARP ticket in our JIRA project with a self-contained repro and we will be happy to investigate with you.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.