IFilteredMongoCollection<T> Ignores Type Filtering in Queries (Behavior Change from v2 to v3)

Hello,

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. The issue is critical for our enterprise application as it affects how queries are executed against a polymorphic document structure.

Reproduction Code:

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 } }

This change in behavior breaks our filtering logic and prevents us from migrating to MongoDB.Driver 3.x. We rely on _t filtering to ensure polymorphic queries return only the expected document types.

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?

Thanks!

Hi @Jaroslaw_Szmigielski ,

Seems like you’re using Hierarchical Discriminators correctly. I can confirm that the issue is reproducible. Thanks for sharing the example. I also see that CSHARP-5528 was created :slight_smile: Our team will have a look soon.

Thanks,

Rishit.