-
-
Save arunoda/ce98cc988041c7779963 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, nice ! Thanks for this.
Do you insert this code inside client/clients.js ?