In CouchDB, how do you find the most common value?
I am trying to classify aggregation levels by finding the most common value of a specific field in documents that are flattened to a given level.
I have documents like this:
{ year: 2012,
month: 01,
category: blue
},
{ year: 2012,
month: 01,
category: blue
},
{ year: 2012,
month: 01,
category: blue
},
{ year: 2012,
month: 01,
category: green
}
The map function basically emits these documents with keys like [year, month]
(although I could include a category if needed). I shrink it to shrink it down to the most common category.
In the case of my above examples, group = false, level_1 and level_2 should all boil down to "blue".
I was thinking about trying to change the key to [year, month, category]
with the hope that I can count the values โโof the categories when I moved the aggregation. But it doesn't work.
How do I find the most common meaning for a category? I feel like the answer is simple, but I just don't connect the dots.
Thank.
source to share
It's simple, but not concise as I worked.
{
"views": {
"most_category": {
"map": "function(doc){
if (doc.category && doc.year && doc.month) {
var hash = {};
hash[doc.category] = 1;
emit([doc.year, doc.month], hash);
}
}",
"reduce": "function(keys, values, rereduce) {
var agg = values[0];
for (var i = 1; i < values.length; ++i) {
for (var category in values[i]) {
if (agg[category]) {
agg[category] += values[i][category];
} else {
agg[category] = values[i][category];
}
}
}
var most_category = null;
var most_count = 0;
for (var category in agg) {
if (most_count<agg[category]) {
most_category = category;
most_count = agg[category];
}
}
var hash = {};
hash[most_category] = most_count;
return hash;
}"
}
}
}
source to share