note 107086 modified in mongocollection.group by danbrown
| From: | danbrown@php.net | Date: | Fri, 06 Jan 2012 19:14:49 +0000 |
| Subject: | note 107086 modified in mongocollection.group by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185407@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.
--was--
Here's my code to do an equivalent of a GROUP BY and a SUM
$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