Skip to content

Instantly share code, notes, and snippets.

@pckujawa
Last active August 29, 2015 13:56
Show Gist options
  • Save pckujawa/9293884 to your computer and use it in GitHub Desktop.
Save pckujawa/9293884 to your computer and use it in GitHub Desktop.
MapReduce, would love a second opinion / another pair of eyes (SO question)
function getUnique() {
var u = {}, a = [];
for (var i = 0, l = this.length; i < l; ++i) {
if (u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
};
function emitHelper(kind, shop, tag) {
emit(shop, {
"kind": kind,
"tag": tag,
});
};
db.customers.mapReduce(
function mapCustomerToShopWithKindAndTag() {
var customer = this;
var kind;
getUnique(this.tags).forEach(function(tag) {
kind = customer.status ? "unique_customers_submitted" : "unique_customers_withheld";
emitHelper(kind, customer.shop, tag.tag);
emitHelper("unique_customers_total", customer.shop, tag.tag);
});
this.tags.forEach(function(tag) {
kind = customer.status ? "total_customers_submitted" : "total_customers_withheld";
emitHelper(kind, customer.shop, tag.tag);
emitHelper("total_customers_total", customer.shop, tag.tag);
});
},
function reduceToCountOfKindsPerTag(key, sets) {
var forms = {};
sets.forEach(function(set) {
if (typeof forms[set.tag] == "undefined") forms[set.tag] = {};
if (typeof forms[set.tag][set.kind] == "undefined") forms[set.tag][set.kind] = 0;
forms[set.tag][set.kind] += 1;
});
return forms;
}, {
out: "form_numbers",
},
function(err, results) {
console.log(err);
//console.log(results);
results.find().toArray(function(err, numbers) {
db.server.close(); // Maybe wrap in a try/catch too
console.log(err);
console.log(JSON.stringify(numbers));
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment