I have a posts document with a tags property, which is stored as a simple array. I'm trying to write a view that returns all the tags, with the # of times they occur, sorted by most occurrences.
The following returns the list, but it isn't sorted. Basically I need to put the reduce results into the key somehow? Please correct me if I'm doing something stupid, and let me know if I should just sort it with my middleware.
// map
function(doc) {
if (doc.tags) {
doc.tags.forEach(function(tag) {
emit(tag, 1);
});
}
}
// reduce
function(keys, values, rereduce) {
return sum(values);
}