|
MongoCollection::groupPerforms an operation similar to SQL's GROUP BY command Description
public array MongoCollection::group
( mixed
$keys
, array $initial
, MongoCode $reduce
[, array $options = array()
] )Parameters
Return ValuesReturns an array containing the result. Changelog
ExamplesExample #1 MongoCollection::group example This groups documents by category and creates a list of names within that category.
<?php The above example will output something similar to: [{"category":"fruit","items":["apple","peach","banana"]},{"category":"veggie","items":["corn","broccoli"]}] Example #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.
<?php The above example will output something similar to: array(4) { ["retval"]=> array(1) { [0]=> array(1) { ["count"]=> float(1) } } ["count"]=> float(1) ["keys"]=> int(1) ["ok"]=> float(1) } Example #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 |