Sokil\Mongo\Pipeline::project PHP Method

project() public method

Passes along the documents with only the specified fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.
public project ( array $pipeline ) : Pipeline
$pipeline array
return Pipeline
    public function project(array $pipeline)
    {
        $this->addStage('$project', $pipeline);
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Check if pipeline added as new or appended to previouse on same operator
  */
 public function testPipeline_ToString()
 {
     $pipeline = new Pipeline($this->collection);
     // insert new match pipeline
     $pipeline->match(array('field1' => 'value1'));
     // insert new project pipeline
     $pipeline->project(array('field2' => 'value2'));
     // insert new match pipeline
     $pipeline->match(array('field3' => 'value3'));
     // append match pipeline to previous
     $pipeline->match(array('field3' => 'value3merged', 'field4' => 'value4'));
     // insert new sort pipeline
     $pipeline->sort(array('field5' => 'value5'));
     // insert new group pipeline
     $pipeline->group(array('_id' => '$groupField', 'field6' => array('$sum' => 1)));
     $validJson = '[{"$match":{"field1":"value1"}},{"$project":{"field2":"value2"}},{"$match":{"field3":"value3merged","field4":"value4"}},{"$sort":{"field5":"value5"}},{"$group":{"_id":"$groupField","field6":{"$sum":1}}}]';
     $this->assertEquals($validJson, $pipeline->__toString());
 }