map and reduce functions work fine as i said on phpfreaks forum, so i won't paste code for it here.
$criteria is date only.
$mongoCursor = $collection->find($criteria);
foreach ($mongoCursor as $key => $value) {
$mapResult = $this->map($value, $groupKey);
$isInCollection2 = $collection2->findOne(array('_id' => $mapResult['_id']));
if (!empty($isInCollection2)) { //If there will be record with this gorup key, the values are being summed with current $mapResult['value];
$mapResult = $this->reduce($mapResult, $city_id, $isInCollection2['value']);
}
$collection2->update(array('_id' => $mapResult['_id']), array('$set' => array('value' => $mapResult['value'])), array('upsert' => true, 'safe' => true));
}
Input structure: ($collection1)
{
"_id" : ObjectId("4e6dfc8a7ba176a952000000"),
"date" : "2011-08-07",
"something" : 0,
"moresmthng" : 1,
"city_id" : 33,
"prog_id" : 1230,
"some_text" : ""
}
output structure: ($collection2)
{
"_id" : {
"date" : "2011-08-07",
"progid" : 1230,
},
"value" : {
"33" : { //this is city_id
"something" : 0,
"some_text" : ""
"moresmthng" : 1,
}
}
}
if one of next records will have the same date and progId (group keys) but different city_id (for example 45) the output structure will look like:
{
"_id" : {
"date" : "2011-08-07",
"progid" : 1230,
},
"value" : {
"33" : { //this is city_id
"something" : 0,
"some_text" : ""
"moresmthng" : 1,
},
"45" : { //this is city_id
"something" : 12,
"some_text" : "blah blah"
"moresmthng" : 111,
}
}
offtopic : couldn't find the edit button:/