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

Add OfType methods to FilterDefinitionBuilder and IAggregateFluent

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.1
    • Component/s: API
    • Labels:
      None
    • Sprint:
      C# Sprint 23

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

        Attachments

          Activity

            People

            Assignee:
            rstam Robert Stam
            Reporter:
            rstam Robert Stam
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: