Created
August 19, 2011 20:49
-
-
Save chrisvaughn/1157969 to your computer and use it in GitHub Desktop.
count dups in mongo with mapReduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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