Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Map reduce

tholder edited this page Dec 23, 2011 · 4 revisions

You can run a map reduce job as follows using a Shanty document model (Model_Mongo_Activities below). Currently looking at ways to make this a bit cleaning in Shanty.

<?php
$id = new MongoId($contact['custom']['id']);
$mapFunction = new MongoCode("function() { if (this.actor.id == \"$id\") emit(this.verb, 1); }");
$reduceFunction = new MongoCode("function(k,v) { var sum=0; for(var i in v) sum += v[i]; return sum; }");
$verbs = Model_Mongo_Activities::getMongoDb()->command(array(
   "mapreduce" => "activities",
   "map" => $mapFunction,
   "reduce" => $reduceFunction,
   "out" => array("inline" => true)));

$this->view->verbs = array();
foreach($verbs["results"] as $verb) {
   $this->view->verbs[$verb["_id"]] = $verb['value'];
}
Clone this wiki locally