Werbung
MongoCollection::group
(PECL mongo >=0.9.2)
MongoCollection::group — Performs an operation similar to SQL's GROUP BY command
Beschreibung
public array MongoCollection::group
( array $keys
, array $initial
, string $reduce
[, array $condition = array()
] )
Parameter-Liste
- keys
-
Fields to group by.
- initial
-
Initial value of the aggregation counter object.
- reduce
-
A function that aggregates (reduces) the objects iterated.
- condition
-
An condition that must be true for a row to be considered.
Rückgabewerte
Returns an array containing the result.
Beispiele
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
$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);
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
array(4) {
["retval"]=>
array(1) {
[0]=>
array(1) {
["count"]=>
float(1)
}
}
["count"]=>
float(1)
["keys"]=>
int(1)
["ok"]=>
float(1)
}