Skip to content

Instantly share code, notes, and snippets.

@normgraham
Created January 30, 2014 19:41
Show Gist options
  • Select an option

  • Save normgraham/8717192 to your computer and use it in GitHub Desktop.

Select an option

Save normgraham/8717192 to your computer and use it in GitHub Desktop.
Collect field name stats over all documents in a collection
map = function () {
totalDocs++;
var C = function(node, prefix) {
for (var inner_prop in node) {
if (typeof node[inner_prop] == 'object') {
emit(prefix + "." + inner_prop, 1);
result = C(node[inner_prop], prop + "." + inner_prop);
}
else if (node.hasOwnProperty(inner_prop) && inner_prop != 'str'){
emit(prefix + "." + inner_prop, 1);
}
}
}
for (var prop in this) {
emit(prop,1);
if (typeof this[prop] == 'object') {
result = C(this[prop], prop);
}
}
}
reduce = function (key, values) {
var total = 0;
values.forEach(function (value) { total += value; });
return total;
}
finalize = function(key, values){
var stats = { totalOccurances: 0, percentCollection: 0, totalDocs: 0};
try {
stats.totalOccurances = values;
stats.percentCollection = Math.round((values / totalDocs) * 100) + "%";
stats.totalDocs = totalDocs;
}
catch(err)
{
stats.totalOccurances = 1;
stats.percentCollection = Math.round((1 / totalDocs) * 100) + "%";
stats.totalDocs = totalDocs;
}
return stats;
}
db.tweets.mapReduce( map , reduce , { out : { inline : true }, scope : { totalDocs: 0 }, finalize: finalize } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment