[CSHARP-3787] Using Where in projection results in incorrect query Created: 06/Aug/21  Updated: 09/Aug/21  Resolved: 09/Aug/21

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

Type: Bug Priority: Unknown
Reporter: Ales Jeusnik Assignee: James Kovacs
Resolution: Duplicate Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-3783 GroupJoin with projection yields inco... Closed

 Description   

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



 Comments   
Comment by James Kovacs [ 09/Aug/21 ]

Hi, ales.jeusnik@inel.com,

We have investigated this issue and it appears to have the same root cause as CSHARP-3783. Both should be resolved by the new LINQ provider. I have closed this issue as a duplicate. Please follow CSHARP-3783 for updates.

Sincerely,
James

Comment by James Kovacs [ 09/Aug/21 ]

Hi, ales.jeusnik@inel.com,

Thank you for reporting this issue. We are investigating and will get back to you soon.

Sincerely,
James

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