[CSHARP-2574] $elemMatch query contains gaps when It's generated based on LINQ Any and predicate contains `DocumentExpression` as operand. Created: 03/Apr/19  Updated: 15/Feb/22  Resolved: 15/Feb/22

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

Type: Bug Priority: Major - P3
Reporter: Dmitry Lukyanov (Inactive) Assignee: Unassigned
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-2348 Search by condition with method Any (... Backlog
Related
related to CSHARP-2557 Change in behaviour of parsing nested... Closed
Epic Link: CSHARP-3615

 Description   

The following LINQ expressions contain gaps in generated `$elemManch` queries.
Note: the provided expressions have wrong queries since they use `DocumentExpression` as an operand in the predicate. If the predicate operands are `FieldExpression`, the bug will not appear.
1. 
Expression with bug in the generated queiry:

x => x.G.Any(g => g.E.I.Any(i => i == "insecure" || i == "igloo"))
//The generated query:
 {G.E.I= { "$elemMatch" : { "$or" : [{ "" : "insecure" }, { "" : "igloo" }] } }}

To compare, the linq expression which doesn't generate the Mongo query with gaps:

x => x.G.Any(g => g.E.I.Any(i => i.Name == "insecure" || i.Name == "igloo")) 
//The generated query:
{G.E.I= { "$elemMatch" : { "$or" : [{ "Name" : "insecure" }, { "Name" : "igloo" }] } }}

Pay attention on expression operands: i - DocumentExpression in the first example and i.Name - FieldExpression from the second.
2.

x => x.G.Any(g => g != null && g.E != null)
//The generated query:
{G= { "$elemMatch" : { "" : { "$ne" : null }, "E" : { "$ne" : null } } }}

3.

//ClassEquals:
x => x.G.Any(g => g == c1 && g != c2)
//where c1 and c2 are objects of some class
//The generated query can be similar to:
//{G=
//{ "$elemMatch" :
//  { "" :
//      {
//          "Ids" : null,
//          "D" : "Dolphin",
//          "X" : null,
//          "$ne" :
//          {
//              "Ids" : null,
//              "D" : "Dolphin",
//              "X" : null
//          }
//      }
//  }
// }}.

NOTE2: It will be a good idea to consider removing the first comparison by `Serializer` type from here.

It looks like the check on `IBsonDocumentSerializer` is problematic and not safe. This condition was the reason for the ticket CSHARP-2557.

   

   


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