[CSHARP-1360] Add OfType methods to FilterDefinitionBuilder and IAggregateFluent Created: 10/Aug/15  Updated: 20/Jan/16  Resolved: 10/Aug/15

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.0.1
Fix Version/s: 2.1

Type: New Feature Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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);



 Comments   
Comment by Githook User [ 10/Aug/15 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1360: Add OfType methods to FieldDefinitionBuilder and IAggregateFluent.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/9aacea634c165332d8b37fdfbbf21e16d2435c35

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