Created
June 9, 2011 05:01
-
-
Save trylik/1016096 to your computer and use it in GitHub Desktop.
MongoDB MapReduce Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conn = new Mongo('localhost'); | |
// access database | |
$db = $conn->benchmark; | |
// access collection | |
$stats = $db->coll3; | |
$map = new MongoCode("function() { emit(this.shop_hash,1); }"); | |
$reduce = new MongoCode("function(k, vals) { ". | |
"var sum = 0;". | |
"for (var i in vals) {". | |
"sum += vals[i];". | |
"}". | |
"return sum; }"); | |
$clicks = $db->command(array( | |
"mapreduce" => "coll3", | |
"map" => $map, | |
"limit" => 100000, | |
"reduce" => $reduce, | |
"out" => array("replace" => "eventCounts"), | |
"verbose" => true | |
)); | |
$statsResult = $db->selectCollection($clicks['result'])->find(); | |
foreach ($statsResult as $stat) { | |
echo "{$stat['_id']} had {$stat['value']} click(s).\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment