Skip to content

Instantly share code, notes, and snippets.

@IrakliJani
Created January 2, 2013 18:42
Show Gist options
  • Save IrakliJani/4436775 to your computer and use it in GitHub Desktop.
Save IrakliJani/4436775 to your computer and use it in GitHub Desktop.
MongoDB simple mapReduce (counts chars in a wordlist) (ugly version)
map = function(){
chars = this.word.split("");
length = chars.length;
result = {};
for(var i = 0; i < length; ++i){
if(! result[chars[i]]) result[chars[i]] = 0;
result[chars[i]] += 1;
}
emit(0, result);
}
reduce = function(key, values){
length = values.length
result = {}
for(var i = 0; i < length; ++i){
for(var key in values[i]){
if(! result[key]) result[key] = 0;
result[key] += values[i][key];
}
}
return result;
}
finalize = function(key, value){
return [key, value];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment