[CSHARP-4935] Linq3Implementation unable to handle interface as root Created: 18/Jan/24  Updated: 05/Feb/24

Status: Investigating
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Luca Strebel Assignee: Robert Stam
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File Program.cs    

 Description   

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.



 Comments   
Comment by Luca Strebel [ 05/Feb/24 ]

robert@mongodb.com Can I get an update on this? I've provided explanations as well as a PR for this issue. Let me know if I can help with anything

Comment by Luca Strebel [ 18/Jan/24 ]

I've just submitted a PR that would solve the described issue https://github.com/mongodb/mongo-csharp-driver/pull/1250

 

 

Comment by PM Bot [ 18/Jan/24 ]

Hi luca@rapidata.ai, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.