(PECL mongo >=0.9.2)
MongoCollection::group — Performs an operation similar to SQL's GROUP BY command
Fields to group by.
Initial value of the aggregation counter object.
A function that aggregates (reduces) the objects iterated.
An condition that must be true for a row to be considered.
Returns an array containing the result.
Example #1 MongoCollection::group() example
<?php
$collection->save(array("a" => 2));
$collection->save(array("b" => 5));
$collection->save(array("a" => 1));
// use all fields
$keys = array();
// set intial values
$initial = array("count" => 0);
// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";
// only use documents where the "a" field is greater than 1
$condition = array("a" => array( '$gt' => 1));
$g = $collection->group($keys, $initial, $reduce, $condition);
var_dump($g);
?>
위 예제의 출력 예시:
array(1) { [0]=> array(1) { ["count"]=> int(1) } }