Hi
I tried switching from driver 2.18 to the newes 2.25. It seems there is a breaking change. In essence switching to 2.25 with LinqProvider kept on V2 generates different query, which in turn breaks most of my projections.
This is the sample code in question:
var projectionTyped = Builders<MyDocument>.Projection.Expression(x => new MyDocument { Id = x.Id, Name = x.Name });
var queryTyped = collection.Aggregate().Project(projectionTyped).Limit(1);
var resultTyped = await queryTyped.FirstOrDefaultAsync();
In 2.18 (and 2.19 as well) this query produces:
aggregate([{ "$project" : { "_id" : 1, "name" : 1 } }, { "$limit" : 1 }])
while in 2.20 - 2.25 it produces:
aggregate([{ "$project" : { "Id" : "$_id", "Name" : "$name", "_id" : 0 } }, { "$limit" : NumberLong(1) }])
I do have a camelCase convention added globally and mongoSettings.LinqProvider = LinqProvider.V2
. Simply changing the driver version breaks the app. Executing the query for 2.18 & 2.19 produces a valid MyDocument
with expected data, while any next driver version returns a document with default values.
What solves it? Switching to anonymous like this:
var projectionAnonymous = Builders<MyDocument>.Projection.Expression(x => new { x.Id, x.Name }); var queryAnonymous = collection.Aggregate().Project(projectionAnonymous).Limit(1); var resultAnonymous = await queryAnonymous.FirstOrDefaultAsync();
But this is not a workable solution for me - I would have to do mappings everywhere.
Is there any setting where I can fix it to work exactly like in 2.18? Generally I want to switch to LinqProvider.V3, but I would like to plan a gradual switch.
Full repro (using mongo ephemeral for easy tests):
anddrzejb/mongoProjectionRepro: Issue reproduction when switching from mongo 2.18 to 2.25 (github.com)