Add OfType methods to FilterDefinitionBuilder and IAggregateFluent.
The OfType methods in FilterDefinitionBuilder support testing that a document or field is of some derived type, and the OfType method in IAggregateFluent supports only passing documents that are of some derived type on to the next stage of the pipeline.
FilterDefinitionBuilder examples:
var builder = Builders<Person>.Filter; builder.OfType<Twin>(); builder.OfType<Twin>(twin => twin.WasBornFirst); // match all first born twins and triplets builder.Or( builder.OfType<Twin>(twin => twin.WasBornFirst), builder.OfType<Triplet>(triplet => triplet.BirthOrder == 1)); // match all persons with a pet Cat (with all 9 lives left in the second example) builder.OfType<Animal, Cat>(person => person.Pet); builder.OfType<Animal, Cat>(person => person.Pet, cat => cat.LivesLeft == 9); // match all people that either have a Cat with all 9 lives left or a lap Dog builder.Or( builder.OfType<Animal, Cat>(person => person.Pet, cat => cat.LivesLeft == 9), builder.OfType<Animal, Dog>(person => person.Pet, dog => dog.IsLapDog));
IAggregationFluent examples:
// use OfType in the middle of an aggregation pipeline var collection = database.GetCollection<C>("test"); var aggregate = collection.Aggregate() .SortBy(c => c.X) .OfType<D>() // D derives from C and adds a Y field .Match(d => d.Y == 2);