note 107086 modified in mongocollection.group by derick
| From: | derick@php.net | Date: | Thu, 29 Nov 2012 13:03:11 +0000 |
| Subject: | note 107086 modified in mongocollection.group by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192195@lists.php.net to get a copy of this message | ||
Here's my code to do an equivalent of a GROUP BY and a SUM
<?php
$contributionCol = $db->customers->contribution;
$group = $contributionCol->group(array('date' => true), array('sum' =>
0), "function (obj, prev) { prev['sum'] += obj.amount; }");
?>
This groups by the 'date' column and sums over the 'amount' column. In my
testing this was much slower than querying all rows and doing the Group by code with PHP. It could
just be my particular setup and data set.
Also at first my amount column was a string, which caused the results to be concatenated rather than
arithmetic addition, something else to watch out for.
[red.: Instead of this, please use the new aggregation framework. See the documentation for
MongoCollection::aggregate]
--was--
Here's my code to do an equivalent of a GROUP BY and a SUM
<?php
$contributionCol = $db->customers->contribution;
$group = $contributionCol->group(array('date' => true), array('sum' =>
0), "function (obj, prev) { prev['sum'] += obj.amount; }");
?>
This groups by the 'date' column and sums over the 'amount' column. In my
testing this was much slower than querying all rows and doing the Group by code with PHP. It could
just be my particular setup and data set.
Also at first my amount column was a string, which caused the results to be concatenated rather than
arithmetic addition, something else to watch out for.
http://php.net/manual/en/mongocollection.group.php