Skip to content

Instantly share code, notes, and snippets.

@arunoda
Created November 2, 2015 09:46
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arunoda/ce98cc988041c7779963 to your computer and use it in GitHub Desktop.
Save arunoda/ce98cc988041c7779963 to your computer and use it in GitHub Desktop.
MapReduce = function (db, sourceColl, outCollection, map, reduce, options) {
var finalize = options.finalize;
var query = options.query;
var mrContext = options.scope || {};
for(var key in mrContext) {
this[key] = mrContext[key];
}
this['emit'] = function() {};
var emittedData = {};
var count = db[sourceColl].find(query).count();
var startAt = Date.now();
print(" need to fetch: " + count);
db[sourceColl].find(query).forEach(function(d) {
var response = map.call(d);
var k = JSON.stringify(response[0]);
if(!emittedData[k]) {
emittedData[k] = [];
}
emittedData[k].push(response[1]);
});
var diff = Date.now() - startAt;
print(" fetched in: " + diff + " ms");
var bulk = db[outCollection].initializeUnorderedBulkOp();
for(var k in emittedData) {
var key = JSON.parse(k);
key.time = new Date(key.time);
var reducedData = reduce(key, emittedData[k]);
var finalValue = finalize(key, reducedData);
bulk.find({_id: key}).upsert().updateOne({$set: {value: finalValue}});
}
startAt = Date.now();
bulk.execute();
diff = Date.now() - startAt;
print(" writing completed in: " + diff + " ms");
};
@maximebodereau
Copy link

Hi, nice ! Thanks for this.
Do you insert this code inside client/clients.js ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment