couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lehnardt <>
Subject Re: reduce fn for grouping items with same key
Date Mon, 10 Aug 2009 07:39:33 GMT
Hi Nitin,

On 10 Aug 2009, at 08:48, Nitin Borwankar wrote:

> Hello all,
> I am trying to write a reduce function which  takes the map fn output
> (k1, v11),
> (k1, v12),
> (k1, v13),
> (k2, v21),
> (k2, v22),
> (k3,v31)
> and produces as a result of the reduce
> {
>  k1: [v11,v12,v13],
>  k2: [v21,v22],
>  k3: [v31]
> }

You shouldn't do this in CouchDB. Reducing needs to truly reduce you  
data set. You are trying to turn tall lists into wide lists. that  
won't work. If you want to get the desired result, make your reduce  
function return null; and query the view with group=true. CouchDB does  
the grouping for you.

> where v11 etc could themselves be lists and do not need to be  
> unpacked in
> the final result but can stay as nested list of lists.
> But I am lost as far as the (keys, values, rereduce) signature.

keys and values contain lists of keys and associated values. The  
reduce step calls you function in two different ways. The first way is  
when the map output gets fed into the reduce function. In this case  
rereduce is set to false.

All other (one to N) invocations of reduce operate on the output the  
previous run produced. This is the default case and rereduce is set to  
true. Your code might want to (or have to) behave differently on the  
first normalization step and the second reduction step and the  
rereduce flag tells you what step you are in.


> So far I have managed by just reusing the return sum(values) on  
> numeric
> values, but this is different and I need some conceptual help with
> reduce/rereduce for non-numeric values and grouping into lists  
> instead of
> taking the sum.
> Thanks much,
> Nitin
> 37% of all statistics are made up on the spot
> -------------------------------------------------------------------------------------
> Nitin Borwankar

View raw message