-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: 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.
- depends on
-
CSHARP-1907 Type discriminators don't work when collection document type is an interface
-
- Ready for Work
-