Question

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.

Was it helpful?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top