tags:

views:

106

answers:

1

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);
}
A: 

It looks like it might be impossible to do with couch alone.

http://osdir.com/ml/couchdb-user/2009-04/msg00081.html

Sean Clark Hess
you are correct. Couch currently can't do what you describe. Sortin in the middle layer shouldn't be too difficult though.
Jeremy Wall