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

LinqProvider V3 produces different (suboptimal) query compared to V2

    • Type: Icon: New Feature New Feature
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.24.0
    • Component/s: Linq, LINQ3, Serialization
    • Labels:
      None
    • Dotnet Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      We are using an "Rich Domain Model" approach in our projects. This means, among other things, that we make use of "ValueObjects" to represent specific concepts in our Domain. These "ValueObjects" are often just wrapper around a string with some behavior, e.g. validation.

      With LinqProvider.V2 a simple query like

      var query = collection.Find(e => e.ValObj == "123x");

      with the document type being

      public class Entity
      {
          public string Id { get; private set; }
          public ValObj ValObj { get; private set; }
          public string Name { get; private set; }
      
          public Entity(ValObj valObj, string name)
          {
              ValObj = valObj;
              Name = name;
          }
      } 

      and the "ValObj" property looking like this (please see attached unit test project for full details)

      public sealed class ValObj : StringValueObject
      {
          public ValObj(string value) : base(value)
          {
              if (!IsValid(value))
              {
                  throw new ArgumentException($"'{value}' is not valid", nameof(value));
              }
      
              Value = value.TrimStart('0');
          }
      
          private static bool IsValid(string val)
          {
              return !string.IsNullOrWhiteSpace(val);
          }
      } 

      would translate to the very sensible

      find({"ValObj": "123x"})
      

      With LinqProvider.V3 the same query translates to

      find({ "$expr" : { "$eq" : [{ "$toString" : "$ValObj" }, "123x"] } }) 

      The query produced by V3 works, but does not take advantage of indexes (apparently).

      How to Reproduce

      Please see attached unit test project.

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            andreas.yiasemi@wienerzeitung.at Andreas Yiasemi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: