Skip to content

Instantly share code, notes, and snippets.

@chrisdavies
Created February 1, 2012 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdavies/1719934 to your computer and use it in GitHub Desktop.
Save chrisdavies/1719934 to your computer and use it in GitHub Desktop.
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
function map() {
var val = {
silver: 0,
gold: 0,
platinum: 0,
total: 1,
email: this.From.Email,
unique: { },
uniqueCount: [this.To._id]
};
if (this.Type == 0) {
val.silver = 1;
} else if (this.Type == 1) {
val.gold = 1;
} else {
val.platinum = 1;
}
emit(this.From._id, val);
}
function reduce(key, values) {
var r = { _id: key, silver: 0, gold: 0, platinum: 0, total: 0, email: '', unique: {}, uniqueCount: [] };
values.forEach(function(v) {
r.silver += v.silver;
r.gold += v.gold;
r.platinum += v.platinum;
r.total += v.total;
r.email = v.email;
v.uniqueCount.forEach(function(u) {
if (!r.unique[u]) {
r.unique[u] = true;
r.uniqueCount.push(u);
}
});
});
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment