mongodb - Need a map reduce function in mongo using php -
need map reduce function mongo in php
this mongo structure
[_id] => mongoid object ( [$id] => 4fcf2f2313cfcd2454500000d ) [id] => 454 [table] => people [news] => array ( [03-06-2012] => 2 [04-06-2012] => 3 [05-06-2012] => 5 [06-06-2012] => 4 )
here try sum array news below code,
$map = new mongocode('function() { emit(this.news, 1); }'); $reduce = new mongocode('function(previous, current) { var count = 0; (index in current) { count = count + current[index]; } return count; }'); $sales = $db->command(array( 'mapreduce' => 'mycollection', 'map' => $map, 'reduce' => $reduce, 'query' => array('table' => 'people'), 'out' => 'news' )); //pr($sales);exit; $users = $db->selectcollection($sales['result'])->find(); foreach ($users $user) { //echo "{$user['_id']} had {$user['value']} sale(s).\n"; pr($user); }
when pr($user)
array ( [_id] => array ( [04-06-2012] => 0 [08-06-2012] => 2 [11-06-2012] => 6 ) [value] => 39540 )
where expected value 8 instead of 39540.
how can correct function , how add field sum array sum of 'news' original collection(mycollection) ?
i not familar map reduce functions in mongo.
when calling emit()
, first parameter key you'll reducing on (or grouping, example). second parameter value being emitted key, can anything. example, mean emit sum of values in news
field, using document's id key:
var map = function() { var total = 0; (count in this.news) { total += count; } emit(this._id, total); }
in case, placeholder reduce function can used (since each emitted key unique, there's little reduction done):
var reduce = function(key, values) { var total = 0; values.foreach(function(v) { total += v; }); return total; }
however, mentioned in google group post, may better off doing pure php:
$cursor = $collection->find(array(), array('news' => 1)); $cursor->snapshot(); foreach ($cursor $document) { $collection->update( array('_id' => $document['_id']), array('$set' => array('sum' => array_sum($document['news']))), array('multiple' => false) ); }
with map/reduce, you'd still have examine results , update records. avoid need execute javascript through mongo, , should more performant. , if can utilize $inc update sums news
field modified on per-document basis, better. above snippet still useful initializing sum
fields across collection, or correcting drift if things out of sync per-document increments.
note: see snapshot() in documentation reasoning behind method call in example above.
Comments
Post a Comment