[CSHARP-2269] Using condition on Linq Aggregation is not working Created: 11/May/18  Updated: 28/Oct/23  Resolved: 15/Feb/22

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

Type: Bug Priority: Critical - P2
Reporter: Liviu Rezmerita Assignee: Unassigned
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, Visual Studio 2017


Attachments: File students.json    
Epic Link: CSHARP-3615

 Description   

I noticed this issue while doing the course on university.mongodb.com

On Homework 3.1 we are requested to remove the smallest score from a list.

I did this query in .net to get all the scores that are greater than the minimum: 

studentCollection.AsQueryable().Select(student => new { student.Id, Scores = student.Scores.Where(sc1 => sc1.Score > student.Scores.Min(sc2 => sc2.Score)) }).ToList();

The generated command is: 

{ "aggregate" : "students", "pipeline" : } } }, "_id" : 0 } }" target="_blank" rel="noopener">{ "$project" : { "Id" : "$_id", "Scores" : { "$filter" : { "input" : "$scores", "as" : "sc1", "cond" : {{color:#d04437} "$gt" : ["$$sc1.score", { "$min" : "$$sc1.scores.score" }] } } }, "_id" : 0 } }, "cursor" : { }, "lsid" : { "id" : CSUUID("8cea95ec-7e89-4752-a3d4-9d7ea628cef9") } }

If you look closely you will notice that the $min condition is wrong, it's using "$$sc1.scores.score" instead of "$scores.score" ($$sc1 = $scores).

 

Also I've tried to do linked Select, but it's not supported:

studentCollection.AsQueryable().Select(student => new { student.Id, MinScore = student.Scores.Min(sc1 => sc1.Score), Scores = student.Scores })
.Select(student => new { student.Id, Scores = student.Scores.Where(sc2 => sc2.Score > student.MinScore) }).ToList();

The Students collection:

{
"_id" : 137,
"name" : "Tamika Schildgen",
"scores" : [
     

Unknown macro: {       "type" }

,
     

Unknown macro: {      "type" }

,
     

Unknown macro: {      "type" }

          ]
}



 Comments   
Comment by James Kovacs [ 15/Feb/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);

Comment by Richard Collette [ 17/Feb/20 ]

There are only 3 critical bugs in this tracker and yet this has sat for 19 months?   I guess that would be fine if this project were solely an open source endeavor but as an employee at a paying Atlas customer, I have to wonder what is going on here?

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