David R
David R

Reputation: 449

Order by in Couchdb

I have a couch database that contains a name and a country field and i have created a view that returns a count per country.

Map function:

function(doc) {
   emit(doc.country, 1);
}

Reduce function:

function(keys, values, rereduce) {
   return sum(values);
}

The problem is that i need to make an "order by", in this case by the count, descending. Any idea how to solve it?

Upvotes: 2

Views: 504

Answers (1)

Ryan Ramage
Ryan Ramage

Reputation: 2616

Unfortunately, with vanilla couchdb, you can't sort the value on the reduce function.

Cloudant, a couchdb hosted provider, offers "chained map reduce" which solves this problem. See http://examples.cloudant.com/sales/_design/sales/index.html

You could do a couple options, one is host your data on cloudant, or two replicate your local data to cloudant to query your order-by.

There is also this: https://github.com/afters/couch-incarnate which unfortunately I have not tried, but might fit for what you are trying to do.

Upvotes: 1

Related Questions