I would love a second opinion / another pair of eyes on this.
db.customers.mapReduce(
function() {
Array.prototype.getUnique = function() {
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;
}
var customer = this;
this.tags.getUnique().forEach(function(tag) {
if (customer.status) {
emit(customer.shop, {
"kind": "unique_customers_submitted",
"tag": tag.tag,
});
} else {
emit(customer.shop, {
"kind": "unique_customers_withheld",
"tag": tag.tag,
});
}
emit(customer.shop, {
"kind": "unique_customers_total",
"tag": tag.tag,
});
})
this.tags.forEach(function(tag) {
if (customer.status) {
emit(customer.shop, {
"kind": "total_customers_submitted",
"tag": tag.tag,
});
} else {
emit(customer.shop, {
"kind": "total_customers_withheld",
"tag": tag.tag,
});
}
emit(customer.shop, {
"kind": "total_customers",
"tag": tag.tag,
});
});
},
function(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) {
console.log(err);
console.log(JSON.stringify(numbers));
db.server.close();
});
}
);