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

Using Where in projection results in incorrect query

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      Using the latest C# driver version (2.13.1) and latest MongoDB community server version (5.0.2).

      Trying to use where in projection yields an incorrect query (besides the already reported issue from CSHARP-3783 GroupJoin with projection yields incorrect query - MongoDB Jira).

      In the example below showing how the driver translated c.Where(ch => ch.ParentLevel == p.Level).All(ch => ch.Status == p.Status), you can see it effectively translated into c.All(ch => ch.ParentLevel == p.Level && ch => ch.Status == p.Status) which is not equivalent logic.

      ChildrenConsistent = (c.Any(ch => ch.ParentLevel == p.Level) || p.Status == SerialStatus.Destroyed)
      && c.Where(ch => ch.ParentLevel == p.Level).All(ch => ch.Status == p.Status)
      
      {
         "ChildrenConsistent":{
            "$and":[
               {
                  "$or":[
                     {
                        "$anyElementTrue":{
                           "$map":{
                              "input":"$c",
                              "as":"ch",
                              "in":{
                                 "$eq":[
                                    "$$ch.ParentLevel",
                                    "$$ch.Level"
                                 ]
                              }
                           }
                        }
                     },
                     {
                        "$eq":[
                           "$Status",
                           5
                        ]
                     }
                  ]
               },
               {
                  "$allElementsTrue":{
                     "$map":{
                        "input":"$c",
                        "as":"ch",
                        "in":{
                           "$and":[
                              {
                                 "$eq":[
                                    "$$ch.ParentLevel",
                                    "$$ch.Level"
                                 ]
                              },
                              {
                                 "$eq":[
                                    "$$ch.Status",
                                    "$$ch.Status"
                                 ]
                              }
                           ]
                        }
                     }
                  }
               }
            ]
         }
      }

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            ales.jeusnik@inel.com Ales Jeusnik
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: