Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-1286

Handle Pipeline&Decoder in Collection::aggregate()

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Builder
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Feature discussed with a user. The idea is to make it possible to pass to Collection::aggregate an object that implements both Pipeline and Decoder. The decoder is then automatically used to process the pipeline results.

      public function aggregate(Pipeline $pipeline, array $options = [])
      {
          if (!isset($options['codec']) && $pipeline instanceof \MongoDB\Codec\Decoder) {
              $options['codec'] = $pipeline;
          }
      
          $this->collection->aggregate($pipeline, $options);
      } 

      The use-case is to create classes that describe both how the data is queried and how it is transformed.

       

      class PipelineAndDecode implements Decoder, PipelineInterface
      {
          use DecodeIfSupported;
      
          public function __construct(string $name) {}
      
          public function getPipeline(): Pipeline
          {
              return new Pipeline(
                  Stage::match(author: $this->name),
                  Stage::project(_id: 0, title: 1, author: 1, body: 1),
                  Stage::limit(10),
              );
          }
      
          public function canDecode($value): bool
          {
              return $value instanceof stdClass;
          }
      
          public function decode($value)
          {
              return new Article(
                  $value->title,
                  $value->author,
                  $value->body,
              );
          }
      }
      
      
      class Repository
      {
          public function __construct(private Collection $collection) {}
          public function aggregate(Pipeline $pipeline, array $options = [])
          {
              if (!isset($options['codec']) && $pipeline instanceof \MongoDB\Codec\Decoder) {
                  $options['codec'] = $pipeline;
              }
      
              $this->collection->aggregate($pipeline, $options);
          }
      } 

            Assignee:
            Unassigned Unassigned
            Reporter:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: