the built-in reduce ("_sum") handles both arrays and objects, so you should be 
able to just do;

map:
function(doc) {
  emit(doc.label, {"base":doc.basePoints, "bonus":doc.bonusPoints});
}

reduce:
_sum

B.

> On 13 Jun 2018, at 20:40, Aurélien Bénel <aurelien.be...@utt.fr> wrote:
> 
> Dear David,
> 
>> I desire that value to be split out by base and bonus points.
> 
> Then you’ll need an array as the key: a first member for the selection and a 
> second one for the grouping.
> 
> 
> MAP :
> 
> function(o) {
>   emit( [o.label, "base"] , o.basePoints);
>   emit( [o.label, "bonus"], o.bonusPoints);
> }
> 
> REDUCE
> 
> _sum
> 
> QUERY
> 
> ?group=true&startkey=["gold"]&endkey=["gold",{}]
> 
> 
> Regards,
> 
> Aurélien 
> 
> 
>> Début du message réexpédié :
>> 
>> De: David Park <david.p...@points.com>
>> Objet: Rép : couchdb erlang reduce - aggregate object
>> Date: 13 juin 2018 à 20:17:19 UTC+2
>> À: user@couchdb.apache.org
>> Répondre à: user@couchdb.apache.org
>> 
>> ok. so here's my test
>> 
>> 3 docs
>> 
>> {"_id": "a", "label": "gold", "basePoints": 1000, "bonusPoints": 2000}
>> {"_id": "b", "label": "gold", "basePoints": 10000, "bonusPoints": 20000}
>> {"_id": "c", "label": "silver", "basePoints": 10000, "bonusPoints": 20000}
>> 
>> Then I took your advice and wrote a javascript (not erlang) view
>> 
>> function(o) {
>>   emit(o.label, o.basePoints);
>>   emit(o.label, o.bonusPoints);
>> }
>> 
>> then threw in a reduce of
>> 
>> _sum
>> 
>> then I queried my view with group = true where key = label
>> And I get a response of
>> {
>> "rows":[
>>   {"key":"gold","value":33000}
>> ]
>> }
>> 
>> I desire that value to be split out by base and bonus points.
>> 
>> David Park   Software Developer

Reply via email to