Skip to content

Instantly share code, notes, and snippets.

@chrisvaughn
Created August 19, 2011 20:49
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 chrisvaughn/1157969 to your computer and use it in GitHub Desktop.
Save chrisvaughn/1157969 to your computer and use it in GitHub Desktop.
count dups in mongo with mapReduce
//count dups in mongo with mapReduce
m = function() { emit(this.user_id, 1); };
r = function (k, vals) { return Array.sum(vals); };
res = db.tasks.mapReduce(m, r, { out : {replace : "results", db : "mr"}});
// more difficult
function map() {
var sorted = this._keywords.sort();
key = sorted.join(" ");
var ret = {title : this.title, id : this._id, total: 1};
emit(key, ret);
}
function reduce (k, values) {
var ret = { total: 0, titles: new Array(), ids: new Array() };
values.forEach(function(value) {
ret.total += value.total;
ret.titles.push(value.title);
ret.ids.push(value.id);
});
return ret;
}
res = db.books_to_buy.mapReduce(map, reduce, { out : {replace : "results", db : "ltl"}});
> use mr
> db.results.find().sort({'values' : -1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment