couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zachary Zolton <zachary.zol...@gmail.com>
Subject Appropriate use of reduce
Date Sat, 29 Jan 2011 16:31:29 GMT
A map-only view keyed by date should be all you need.

You could then transform it to the format needed for graphing in your
client program, or by using a list function:

http://v.gd/ZrUBCC

-Zach

On Saturday, January 29, 2011, Robert Newson <robert.newson@gmail.com> wrote:
> It will stop working when you have enough documents as, as you note,
> you don't reduce in your reduce function (and reducing is mandatory).
>
> You don't need a reduce at all to 'collate by key'. Remove your reduce
> and query with ?key=<name of key> and you'll get all rows for that
> value.
>
> B.
>
> On Sat, Jan 29, 2011 at 9:03 AM, Perryn Fowler <pezlists@gmail.com> wrote:
>> Hi there,
>>
>> I have a heap of documents that record several measurements for a
>> given day - for example for temperatures one might look like
>>
>> {
>>  "_id" : "whatever",
>>  "_rev" : "whatever",
>>  "date" : "29/1/2011",
>>  "hi-temp" : 30
>>  "low-temp" : 16
>> }
>>
>> I would like to generate graphs from this data, and my graphing
>> library wants it in this format
>>
>> {
>>  "hi-temp" : [ [ "29/1/2011", 30], [ "28/1/2011", 32], [ "27/1/2011",
>> 29], ...etc ],
>>  "low-temp" : [ [ "29/1/2011", 16], [ "28/1/2011", 17], [
>> "27/1/2011", 10], ...etc ]
>> }
>>
>> So, I wrote the following map function
>>
>> function(doc) {
>>        function(doc) {
>>           for ( var key in doc) {
>>             if (key[0] != '_' && key != "date") {
>>               emit(key,[doc.date,doc[key]])
>>             }
>>           }
>>        }
>> }
>>
>> and the following reduce function
>>
>> function(keys, values, rereduce) {
>>  return values;
>> }
>>
>> and that all seems to work :)
>>
>> Two things bother me though...
>>
>> 1) I suspect that if a re-reduce call happened things would break
>> because I would end up with triple nested arrays (rather than double)
>>   so perhaps I should be flattening the values array before returning
>> on a re-reduce ? But I am not sure, and so far I dont seem to be
>> getting any re-reduce calls.
>>  Is there any way of forcing re-reduces to happen so I can test?
>>
>> 2) I'm not actually reducing in the reduce function, just using it to
>> collate by key - and CouchDB docs are to be littered with warnings
>> against doing that sort of thing.
>>    Is there some other way I should be doing this with couch? Or is
>> couch a poor fit for this problem?
>>
>> cheers
>> Perryn
>>
>

Mime
View raw message