Skip to content

Instantly share code, notes, and snippets.

@ngmaloney
Created December 17, 2010 05:52
Show Gist options
  • Save ngmaloney/744545 to your computer and use it in GitHub Desktop.
Save ngmaloney/744545 to your computer and use it in GitHub Desktop.
Mongodb Max Length MapReduce
reduce = function(key,vals) {
return vals.sort(function(a,b){return b - a})[0];
}
map = function() {
var map_send = function(k,v) {
emit(k,v.toString().length);
}
var recursive_mapper = function(obj,level) {
level = level || 0;
for(key in obj) {
if(level > 3) {
return false;
}
var type = typeof(obj[key]);
switch(type) {
case 'string':
case 'number':
map_send(key,obj[key]);
break;
case 'object':
level++;
recursive_mapper(obj[key],level);
break;
}
}
}
recursive_mapper(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment