Skip to content

Instantly share code, notes, and snippets.

@ackuser
Forked from nherment/gist:1431054
Created October 27, 2016 11:17
Show Gist options
  • Save ackuser/e6045b4c4e0196aa526b07fc7ef9c949 to your computer and use it in GitHub Desktop.
Save ackuser/e6045b4c4e0196aa526b07fc7ef9c949 to your computer and use it in GitHub Desktop.
MapReduce with MongoDB in NodeJS
var doMapReduce = function(options, callback) {
var map = function () {
var dateKey = new Date(options.time.getTime());
dateKey.setMinutes(0);
dateKey.setSeconds(0);
dateKey.setMilliseconds(0);
var mapped = {
user: this.user,
type: this.type,
time: dateKey,
value: this.value,
weight: 1
}
var key = options.userEmail + ":" + options.device.uid + ":" + dateKey;
emit(key, mapped);
};
var reduce = function (key, values) {
var reduced = {
user: values[0].user,
type: values[0].type,
time: values[0].hour,
value: 0,
weight: 0
}
var totalWeight = 0;
var sum = 0;
values.forEach(function(value) {
sum += (value.value * value.weight);
totalWeight += value.weight;
});
// TODO: average in finalize to spare CPU (?)
reduced.value = sum/totalWeight;
reduced.weight = totalWeight;
return reduced;
};
var finalize = function(key, value) {
return value;
}
//TODO: criteria on sensor and user
var options = {
query: {
user : options.userEmail,
type : options.device
},
out: {
merge: "measure1hs" // lowercase and 's' appended b/c not using Mongoose here...
},
include_statistics: true,
verbose: true
};
db.Measure.collection.mapReduce(map, reduce, options, function(err, collection, stats) {
callback(err, stats);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment