Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1360

Add OfType methods to FilterDefinitionBuilder and IAggregateFluent

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.1
    • Affects Version/s: 2.0.1
    • Component/s: API
    • None

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

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: