When you have a class MyClassA
and MyClassB
, both implement from IMyInterface
and decorated with [BsonDiscriminator]
, and then try to GetCollection and OfType, the resulting query ignores _t
.
To illustrate, quick not-really-pseudo-code, as actual code is a tad more complex, but this should work just as well:
public interface IMyInterface { }
[BsonDiscriminator("MyClassA")]
public class MyClassA : IMyInterface { }
[BsonDiscriminator("MyClassB")]
public class MyClassB : IMyInterface { }
await LoadAsync<MyClassB>();
public async Task<T> LoadAsync() where T : IMyInterface
{
IMongoCollection<T> collection = database.GetCollection<T>(collectionName);
string query = collection.Aggregate().OfType<TSetting>().ToString();
// query in 3.0 = aggregate([{ "$match" : { } }])
// query in 2.30 = aggregate([{ "$match" : { "_t" : "MyClassB" } }])
T result = await collection.Aggregate().OfType<TSetting>().FirstOrDefaultAsync();
// result in 3.0 is MyClassA
// result in 2.30 is MyClassB
}
Same happens when using Aggregate().Match(item => item is MyClassB)
.
In a different place, I use a base class that is also mapped with IsRootClass true - and there the query seems to be correct.
This is a significant breaking change that has not been documented and caused serious bugs in my application. I am forced to rollback to 2.30 until this is resolved, or a workaround is provided.