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

Several expressions no longer translate as expected, even in v2

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.19.0
    • Component/s: None
    • None
    • 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

      Since 2.19 now defaults to LINQ V3, I now have several broken queries, which I expected. However, some of them still fail when I switch back to the LINQ V2 translator.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      2.19

      How to Reproduce

      Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

       

      private static string GetFieldName<TDocument>(Expression<Func<TDocument, object>> expression)
          where TDocument : class
      {
          var def = new ExpressionFieldDefinition<TDocument, object>(expression);
          var rendered = def.Render(BsonSerializer.LookupSerializer<TDocument>(), BsonSerializer.SerializerRegistry, true);
          return rendered.FieldName;
      }
      
      [Fact]
      public void GetFieldName_NestedValue()
      {
          var field = GetFieldName<Foo>(x => x.Subdocument.NestedProperty);
          Assert.Equal("Subdocument.NestedProperty", field);
      }
      
      [Fact]
      public void GetFieldName_ArrayWithIndex()
      {
          var field = GetFieldName<Foo>(a => a.ArrayField[-1]);
          Assert.Equal("ArrayField.$", field);
      } 

       

       

      Additional Background

      The $ array one tells me to use `.FirstMatchingElement()` instead, which is fine for transitioning to v3 but I didn't expect v2 to start failing.

      The nested property one throws :

      MongoDB.Driver.Linq.ExpressionNotSupportedException
      Expression not supported: Convert(x.Subdocument.NestedProperty, Object).

      It starts working if I pass in a 2nd type to my helper for the field type to use instead of `object`...but I don't want to have to specify the type every time.

       

      These changes in behavior on V2 lead to me having to schedule time to do a thorough audit of our use of Mongo to avoid runtime problems that may not be caught by tests.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            fred.morel@onepeloton.com Fred Morel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: