group() public method

Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. The output documents contain an _id field which contains the distinct group by key. The output documents can also contain computed fields that hold the values of some accumulator expression grouped by the $groupā€˜s _id field. $group does not order its output documents.
public group ( array | callable $stage ) : Pipeline
$stage array | callable
return Pipeline
Example #1
0
 public function testSubtract_CallableExpressin()
 {
     $pipeline = new Pipeline($this->collection);
     $pipeline->group(function ($stage) {
         /* @var $stage \Sokil\Mongo\Pipeline\GroupStage */
         $stage->setId('user.id')->sum('totalAmount', function ($expression) {
             /* @var $expression \Sokil\Mongo\Pipeline\Expression */
             $expression->subtract(function ($expression) {
                 $expression->multiply('$amount', 3.15);
             }, 0.95);
         });
     });
     $this->assertEquals('[{"$group":{"_id":"user.id","totalAmount":{"$sum":{"$subtract":[{"$multiply":["$amount",3.15]},0.95]}}}}]', (string) $pipeline);
 }
Example #2
0
 public function testPush()
 {
     $this->collection->insertMultiple(array(array("_id" => 1, "item" => "abc", "price" => 10, "quantity" => 2, "date" => new \MongoDate(strtotime("2014-01-01T08:00:00Z"))), array("_id" => 2, "item" => "jkl", "price" => 20, "quantity" => 1, "date" => new \MongoDate(strtotime("2014-02-03T09:00:00Z"))), array("_id" => 3, "item" => "xyz", "price" => 5, "quantity" => 5, "date" => new \MongoDate(strtotime("2014-02-03T09:05:00Z"))), array("_id" => 4, "item" => "abc", "price" => 10, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T08:00:00Z"))), array("_id" => 5, "item" => "xyz", "price" => 5, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T09:05:00Z"))), array("_id" => 6, "item" => "xyz", "price" => 5, "quantity" => 5, "date" => new \MongoDate(strtotime("2014-02-15T12:05:10Z"))), array("_id" => 7, "item" => "xyz", "price" => 5, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T14:12:12Z")))));
     $pipeline = new Pipeline($this->collection);
     $pipeline->group(function ($stage) {
         /* @var $stage \Sokil\Mongo\Pipeline\GroupStage */
         $stage->setId('$item')->push('itemsSold', array('quantity' => '$quantity', 'price' => '$price', 'quantityPrice' => array('$multiply' => array('$quantity', '$price'))));
     });
     $result = $pipeline->aggregate();
     $this->assertEquals('xyz', $result[0]['_id']);
     $this->assertEquals(5, $result[0]['itemsSold'][0]['quantity']);
     $this->assertEquals(5, $result[0]['itemsSold'][0]['price']);
     $this->assertEquals(25, $result[0]['itemsSold'][0]['quantityPrice']);
 }
Example #3
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());
 }