I need to understand a MongoCollection->group() feature. PHP manual for group() states what follows:
<?php
$collection->insert(array("category" => "fruit", "name" => "apple"));
$collection->insert(array("category" => "fruit", "name" => "peach"));
$collection->insert(array("category" => "fruit", "name" => "banana"));
$collection->insert(array("category" => "veggie", "name" => "corn"));
$collection->insert(array("category" => "veggie", "name" => "broccoli"));
$keys = array("category" => 1);
$initial = array("items" => array());
$reduce = "function (obj, prev) { prev.items.push(obj.name); }";
$g = $collection->group($keys, $initial, $reduce);
echo json_encode($g['retval']);
?>
with a result array similar to:
[{"category":"fruit","items":["apple","peach","banana"]},{"category":"veggie","items":["corn","broccoli"]}]
now, I see that the MongoCode reduce() function pushes the item's name into the result array. But what if I want to push one more field besides the name, i.e. the fruit's color?
basically, what I need is to have an output like this:
[{"category":"fruit","items":[["apple", "red"],["peach", "peach"],["banana", "yellow"]]},{"category":"veggie","items":[["corn", "yellow"],["broccoli", "green"]]}]
so I think I should push() somehow differently... how exactly? I can't find the right syntax anywhere.
thanks in advance and sorry for my poor english :-)
Francesco