incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nebu Pookins <nebupook...@gmail.com>
Subject Re: Grouping, with order, and aggregate
Date Mon, 11 Apr 2011 16:47:56 GMT
On Thu, Apr 7, 2011 at 1:43 AM, He Shiming <heshiming@gmail.com> wrote:
> 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;
> SELECT COUNT(DISTINCT author) FROM books;
>
> I consulted http://guide.couchdb.org/draft/cookbook.html , and the
> first query looked like this (with group=true&limit=100):
> map: function(doc) {
>  if (doc.type == 'book' && doc.author)
>    emit(doc.author, 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.

Given that the same author may have written multiple books, you need
to define what the semantics are for "reducing" multiple release
dates. It sounds like for each author, you want the date of their most
recently released book, so your reduce function should probably look
at all of the release dates for a given, return the most recent date
(this is in addition to the summing logic you've already got in place
to count how many books an author has written).

- Nebu

Mime
View raw message