incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Hendricks <mich...@ndrix.org>
Subject Re: using reduce to get SQL distinct like results
Date Tue, 26 Aug 2008 21:53:16 GMT
On Tue, Aug 26, 2008 at 05:01:56PM -0400, Joel Reed wrote:
> I have a bunch of documents that look vaguely like this:
>
> [{ "User":"Jane Doe", "Date":"2008/08/12", } { "User":"Jane Doe", 
> "Date":"2008/08/15", }, etc...]
>
> IOW, There might be several entries for each user all with different dates.
>
> I'd like tp combine all "Jane Doe's" records into 1 entry. Some kind of 
> output like:
>
> "rows": [ {"key": "Jane Doe", "value": [
> {"id":"481bf9e8a0c23bb61eeed4b3707bae59","Date":"2008/08/12"},
> {"id":"1e6e541b391efcb9c137d7097e7de6ed","Date":"2008/08/15"}
> ] } ]

How about something like this:

 "all": {
   "map": "function(doc) { emit(doc.User, doc.Date); }",
   "reduce": "function(keys, values) {
        var results = new Array();
        for ( var i in values ) {
            results.push({
                id   : keys[i][1],
                Date : values[i]
            });
        }

        return results;"
  }
}

When I fetch the view with group=true, that gives me output like:

{"rows":[{"key":"Jane Doe","value":[
{"id":"3f35758bf54e24dcc14ddccbe9a68412","Date":"2008\/08\/12"},
{"id":"17c69a1298f0218f68e94c9912be160f","Date":"2008\/08\/15"}
]}]}

-- 
Michael

Mime
View raw message