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

$elemMatch query contains gaps when It's generated based on LINQ Any and predicate contains `DocumentExpression` as operand.

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Linq
    • None

      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.

         

         

            Assignee:
            Unassigned Unassigned
            Reporter:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: