Skip to content

Instantly share code, notes, and snippets.

@trylik
Created June 9, 2011 05:01
Show Gist options
  • Save trylik/1016096 to your computer and use it in GitHub Desktop.
Save trylik/1016096 to your computer and use it in GitHub Desktop.
MongoDB MapReduce Example
<?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