|
MongoCollection::groupPerforms an operation similar to SQL's GROUP BY command Beschreibung
public array MongoCollection::group
( mixed
$keys
, array $initial
, MongoCode $reduce
[, array $options = array()
] )Parameter-Liste
RückgabewerteReturns an array containing the result. Changelog
BeispieleBeispiel #1 MongoCollection::group example This groups documents by category and creates a list of names within that category.
<?phpDas oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
[{"category":"fruit","items":["apple","peach","banana"]},{"category":"veggie","items":["corn","broccoli"]}]
Beispiel #2 MongoCollection::group example This example doesn't use any key, so every document will be its own group. It also uses a condition: only documents that match this condition will be processed by the grouping function.
<?phpDas 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)
}
Beispiel #3 Passing a If you want to group by something other than a field name, you can pass a function as the first parameter of MongoCollection::group and it will be run against each document. The return value of the function will be used as its grouping value. This example demonstrates grouping by the num field modulo 4.
<?php |