-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Dotnet Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Summary
We’ve encountered a regression in MongoDB.Driver 3.x when using IFilteredMongoCollection<T>. Specifically, when calling .OfType<T>() on a collection and subsequently executing a query, the expected filtering on _t is missing, also when using HierarchicalDiscriminatorConvention.
This behavior differs from MongoDB.Driver 2.x, where the generated query correctly includes _t for filtering by type.
How to Reproduce
using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using MongoDB.Driver.Core.Events; BsonClassMap.RegisterClassMap<Document>(); BsonClassMap.RegisterClassMap<Animal>(); BsonClassMap.RegisterClassMap<Human>(); BsonClassMap.RegisterClassMap<Hawk>(); var connectionString = "mongodb://localhost/db?directConnection=true"; var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString)); settings.ClusterConfigurator = builder => { builder.Subscribe<CommandStartedEvent>(e => { Console.WriteLine($"Command {e.CommandName} -> {e.Command.ToJson()}"); }); }; var client = new MongoClient(settings); // Bootstrap data var collection = client.GetDatabase("db").GetCollection<Animal>("animals"); var human = new Human { Legs = 4, Eyes = 2 }; var hawk = new Hawk { Legs = 4, Wings = 2 }; await collection.InsertManyAsync([human, hawk]); // Query using filtered collection var filteredCollection = client.GetDatabase("db").GetCollection<Human>("animals").OfType<Human>(); var document = await filteredCollection.Find(Builders<Human>.Filter.Eq(x => x.Legs, 4)).ToListAsync(); // Model definitions public class Document { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } } [BsonDiscriminator(RootClass = true)] public class Animal : Document { public int Legs { get; set; } } public class Human : Animal { public int Eyes { get; set; } } public class Hawk : Animal { public int Wings { get; set; } }
Observed Behavior:
Using MongoDB.Driver 2.30.0, the generated query is:
{ "find": "animals", "filter": { "_t": "Human", "Legs": 4 } }
However, with MongoDB.Driver 3.2.1, the _t discriminator is missing:
{ "find": "animals", "filter": { "Legs": 4 } }
Can you confirm whether this is an intentional change or a regression? If intentional, what is the recommended way to preserve _t filtering in queries when using IFilteredMongoDbCollection?