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

"is" operator throwing error after upgrade to MongoDB.Driver 3.0

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

      Using the "is" operator in a LINQ query throws an error after upgrading the MongoDB.Driver library to version 3.0.

      I am using a local standalone server running MongoDB 6.0.6 Community.

      How to Reproduce

      The following code produces the error:

      var client = new MongoClient(connectionString);
      
      BsonClassMap.RegisterClassMap<Animal>(map =>
      {
          map.AutoMap();
          map.AddKnownType(typeof(Dog));
          map.AddKnownType(typeof(Cat));
      });
      
      var collection = client.GetDatabase("test").GetCollection<Animal>("animals");
      
      collection.InsertMany(new Animal[] { new Dog(), new Dog(), new Cat() });
      
      var dogs = collection.AsQueryable().Where(animal => animal is Dog).ToList();
      
      Debug.Assert(dogs.Count == 2);
      
      public class Animal
      {
          public ObjectId Id { get; set; }
      }
      
      public class Dog : Animal { }
      
      public class Cat : Animal { }
      

      Running this code in version 2.30 of the MongoDB.Driver works as expected with no error.

      Running the code in version 3.0 produces this error:

      MongoDB.Driver.Linq.ExpressionNotSupportedException
        HResult=0x80131515
        Message=Expression not supported: (animal Is Dog) because is operator is not supported with the configured discriminator convention.
        Source=MongoDB.Driver
        StackTrace:
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.TypeIsExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, TypeBinaryExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateUsingAggregationOperators(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.Translate(TranslationContext context, Expression expression, Boolean exprOk)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateLambda(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.WhereMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression, ExpressionTranslationOptions translationOptions)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute()
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
      

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            reed@epactnetwork.com Reed Esler
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: