couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From He Shiming <>
Subject Grouping, with order, and aggregate
Date Thu, 07 Apr 2011 05:43:34 GMT
Dear Community,

I'm trying to figure out how to write map/reduce for these two scenarios:

SELECT DISTINCT author FROM books ORDER BY releasedate;

I consulted , and the
first query looked like this (with group=true&limit=100):
map: function(doc) {
  if (doc.type == 'book' &&
    emit(, 1);
reduce: function(keys, values) {
  return sum(values);

I couldn't figure out how to sort by date based on this map function.
I would like to provide a list of authors, with newest released books
at front. Pagination is going to be a plus if possible.

And because I specified "group=true", I'm not getting the total row
count (number of distinct authors, not their books). Reduce function
will work by group. So the only solution I thought of, is to emit all
authors of books (without grouping), then manually count the array in
reduce with 2 "for loops". I'm imagining a fairly large data set, and
this can be slow, and occupy a lot of memory. What are the other

Best regards,
He Shiming

View raw message