Skip to content

Instantly share code, notes, and snippets.

@ymurase
Created December 26, 2013 08:35
Show Gist options
  • Save ymurase/8131297 to your computer and use it in GitHub Desktop.
Save ymurase/8131297 to your computer and use it in GitHub Desktop.
sample of mongoDB map_reduce
var conn = new Mongo();
var db = conn.getDB("oacis_development");
var map = function() {
// var key = {param1: this.v["L"], param2: this.v["T"] };
var key = [ this.v["beta"], this.v["h"] ];
if( this.runs_status_count_cache ) {
var cache = this.runs_status_count_cache;
var total_runs = 0;
for(var stat in cache) {
if (stat == "cancelled") continue;
total_runs += cache[stat];
}
var val = {finished: cache["finished"], total: total_runs };
emit(key, val);
}
else {
emit(key, { ids: [this._id]} );
}
}
var reduce = function(key,values) {
var reduced = {finished: 0, total: 0, ids: []};
values.forEach( function(v){
if( v.finished ) { reduced.finished += v.finished; }
if( v.total ) { reduced.total += v.total; }
if( v.ids ) { reduced.ids = reduced.ids.concat(v.ids); }
});
return reduced;
}
var result = db.parameter_sets.mapReduce(map, reduce,
{ query: {simulator_id: ObjectId("522d751f899e533149000002")}, out: {inline:1} }
);
shellPrint(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment