[CSHARP-4181] Max inside the lamda expression MQL translation is wrong Created: 23/May/22  Updated: 28/Oct/23  Resolved: 26/May/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.14.0

Type: Bug Priority: Major - P3
Reporter: Sudhesh Gnanasekaran Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The max inside the lambda expression is not working as expected in the c# fluent aggregate syntax.

C# driver version: 2.15.1

 db.remittance.insert({Payments: [{Date: ISODate("2020-06-01 04:22:59.948Z")}, \{Date: ISODate("2020-06-01 05:22:59.948Z")}, \{Date: ISODate("2020-06-01 06:22:59.948Z")}]})

var query = coll.Aggregate() .Project(x => new ProjectResult

{ Payment = x.Payments == null ? null : x.Payments.First(y => y.Date == x.Payments.Max(y => y.Date)) }

);

Actual :

db.remittance.aggregate([{ “$project” : { “Payment” : { “$cond” : { “if” :

{ “$eq” : ["$Payments", null] }

, “then” : null, “else” : { “$arrayElemAt” : [{ “$filter” : { “input” : “$Payments”, “as” : “y”, “cond” : { “$eq” : ["$$y.Date",

{ “$max” : *“$$y.Payments.Date”* }

] } } }, 0] } } }, “_id” : 0 } }])

Expected:

db.remittance.aggregate([{ “$project” : { “Payment” : { “$cond” : { “if” :

{ “$eq” : ["$Payments", null] }

, “then” : null, “else” : { “$arrayElemAt” : [{ “$filter” : { “input” : “$Payments”, “as” : “y”, “cond” : { “$eq” : ["$$y.Date",

{ “$max” : *“$$Payments.Date”* }

] } } }, 0] } } }, “_id” : 0 } }])

 

I am trying to get the recent transaction by the date using the max function inside the lambda expression and I am not getting the expected result by this wrong translated query.



 Comments   
Comment by Sudhesh Gnanasekaran [ 27/May/22 ]

Hi Robert,

Will there be any problem to use the linq3 settings because our codebase is already in production with the mongo 4.4 and c# driver version 2.13?

 

Regards,

Sudhesh

Comment by Robert Stam [ 26/May/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

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

I have confirmed that LINQ3 generates the following query for the supplied LINQ expression:

{ $project : { Payment : { $cond : { if : { $eq : ['$Payments', null] }, then : null, else : { $arrayElemAt : [{ $filter : { input : '$Payments', as : 'y', cond : { $eq : ['$$y.Date', { $max : '$Payments.Date' }] } } }, 0] } } }, _id : 0 } }

Comment by Sudhesh Gnanasekaran [ 25/May/22 ]

I updated the wrong translated query and this is the expected translation.

db.remittance.aggregate([{ “$project” : { “Payment” : { “$cond” : { “if” :
{ “$eq” : ["$Payments", null] }
, “then” : null, “else” : { “$arrayElemAt” : [{ “$filter” : { “input” : “$Payments”, “as” : “y”, “cond” : { “$eq” : ["$$y.Date",
{ “$max” : "$Payments.Date” }
] } } }, 0] } } }, “_id” : 0 } }])
 

 

Generated at Wed Feb 07 21:47:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.