Created
January 2, 2013 18:42
-
-
Save IrakliJani/4436775 to your computer and use it in GitHub Desktop.
MongoDB simple mapReduce (counts chars in a wordlist) (ugly version)
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
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