Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2269

Using condition on Linq Aggregation is not working

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 2.14.0
    • Affects Version/s: 2.6.0
    • Component/s: LINQ
    • None
    • Environment:
      Windows 10, Visual Studio 2017
    • None
    • None
    • None
    • None
    • None
    • None

      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" }

                ]
      }

        1. students.json
          52 kB
          Liviu Rezmerita

            Assignee:
            Unassigned Unassigned
            Reporter:
            liviu_rezmerita@dellteam.com Liviu Rezmerita
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: