Skip to content

Instantly share code, notes, and snippets.

@samyakbhuta
Created May 27, 2011 16:25
Show Gist options
  • Save samyakbhuta/995619 to your computer and use it in GitHub Desktop.
Save samyakbhuta/995619 to your computer and use it in GitHub Desktop.
Looking for a implementation solution for a pattern, whose name I don't know yet !!!
----------------- Formation of a game----------------------
db.games.save({
"_id" : ObjectId("4ddf595fb57f547120000002")
"pi" : "3831F237040C4BC083420EE4EC5FAE85",
"di" : "76280583D2DA4903A0336C85AA3B3DE8468E21F8",
"np" : 92,
"p1" : "n",
});
----------------- Formation of a game ends-----------------
-----------Map Reduce Operation-------------------------------------
// Step 2 :
// Update all the "not processed" games to "in process".
//
db.games.update({p1:"n"},{$set:{p1:"i"}},false,true);
//
// The map function
//
var map_players_points_total = function() {
emit(this.pi, {np: this.np, di: this.di});
};
//
// The reduce function, add up all the points for each key (here player).
//
var reduce_players_points_total = function(key,values){
total_points_gained = 0;
device_id = values[0].di;
values.forEach(function(i){
total_points_gained += i.np;
});
return {np:total_points_gained, di: device_id };
};
// Step 3 and 4 :
// Performing map-reduce.
// Note that 'out' type is set to "reduce".
// 'query' is only to select those games that have been marked as "in process" using p1 field.
// Those fulfilling the criteria are given to map function as input.
// Once they have been performed upon, they are marked to "yes, processed", in next statment.
//
var result_players_points_total = db.games.mapReduce(map_players_points_total //Step 4
,reduce_players_points_total
,{
query:{p1:"i"} // Step 3
,out: {reduce:"players_points_total"}
});
// Step 5:
// Update all the "in process" games to "yes, processed" .
//
db.games.update({p1:"i"},{$set:{p1:"y"}},false,true);
-----------Map Reduce Operation Ends -------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment