3

Given that I have some number of documents in my CouchDB database, I would like to be able to get a list of tags ordered by the number of documents associated with each tag. Documents have the following structure:

{
    "name": "Document Name 1",
    "description": "Document description",
    "tags": ["tag1", "tag2", "tag3"]
}

I can obviously create a view that would list each tag and a number of documents associated with each tag looking like the following:

function(doc) {
  if (doc.type == "Document") {
    for (tag in doc.tags) {
      emit(tag, 1)
    }
  }
};
function(keys, values, rereduce) {
  return sum(values);
}

Problem with this approach is that tag names would be keys in this result set and thus I won't be able to sort by the number of documents as well as limit the result to the top X tags. I feel I'm missing something very obvious but still do not see the solution.

Alexander Finn
  • 781
  • 4
  • 11

1 Answers1

2

Here are a couple of links that may help you:

Sorting CouchDB Views By Value

http://geekiriki.blogspot.com/2010/08/couchdb-using-list-functions-to-sort.html

Community
  • 1
  • 1
Ryan Ramage
  • 2,606
  • 18
  • 17
  • Thanks for the links, this is helpful. Sorting in memory or the list function won't work for me several reasons, including the size of the dataset but replicating this data to a separate DB or document would work. – Alexander Finn Nov 23 '11 at 06:15