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

Linq3Implementation unable to handle interface as root

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

      Summary

      When using LinqV3 to translate a linq expression on a mongo collection that has an interface as the generic type. it is unable to translate the query to a mongo query.

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

      Using the latest 2.23.1

      How to Reproduce

      The following code snippet is enough to reproduce the issue. LinqV2 works as expected. 

      using MongoDB.Bson;
      using MongoDB.Bson.Serialization;
      using MongoDB.Bson.Serialization.Conventions;
      using MongoDB.Bson.Serialization.Serializers;
      using MongoDB.Driver;

      public interface IAnimal
      {{{}}
      {{public ObjectId Id { get; set; }}}
      {{public int Age { get; set; }}}
      {{public string Name { get; set; }}}
      }

      public class Bear : IAnimal
      {{{}}
      {{public ObjectId Id { get; set; }}}
      {{public int Age { get; set; }}}
      {{public string Name { get; set; }}}

      {{public bool IsAggressive { get; set; }}}
      }

      public class Program
      {{{}}
      public static void Main(params string[] args)
      {{{}}
      BsonSerializer.RegisterSerializer(typeof(object), new ObjectSerializer(t => true));
      BsonClassMap.RegisterClassMap<Bear>();
      BsonSerializer.RegisterDiscriminatorConvention(typeof(IAnimal), new HierarchicalDiscriminatorConvention("_t"));

      var settings = new MongoClientSettings()
      {{{}}
      //LinqProvider = LinqProvider.V2
      };
      var client = new MongoClient(settings);
      var collection = client.GetDatabase("test").GetCollection<IAnimal>("animal");

      collection.DeleteMany(x => true);

      collection.InsertOne(new Bear()
      {{{}}
      Id = ObjectId.GenerateNewId(),
      Age = 1,
      Name = "Bob the bear",
      IsAggressive = true,
      });

      var bears = client.GetDatabase("test").GetCollection<IAnimal>("animal").Find(x => x is Bear && ((Bear)x).IsAggressive).ToList();

      foreach (var cat in bears)
      {{{}}
      {{Console.WriteLine(cat.Name); }}
      }
      }
      }

      Additional Background

      I've been spending a lot of time trying to make MongoDB work with interface discriminators. This bug is one of the last pieces missing from within the MongoDB driver in order for me to fix this. Additionally I know how to fix it and will create a PR for it.

        1. Program.cs
          2 kB
          Luca Strebel

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            luca@rapidata.ai Luca Strebel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: