couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zachary Zolton <zachary.zol...@gmail.com>
Subject Re: Newbie question: How to collate sum() with other properties
Date Sun, 08 Jan 2012 03:34:32 GMT
Tobi,

I think it's better to get tricky with the keys emitted, and keep the
reduction using the built-in Erlang implementations. How about this?

map:

function(doc) {
  var key;
  switch (doc.type) {
      case 'poll':
        key = [doc._id, 0, doc.title];
      case 'vote':
        key = [doc.poll_id, 1, doc.choice];
  }
  emit(key, null);
}

reduce:

_count

Then, and query using group=true, and you'll get a result like this:

{"rows":[
{"key":["d193f1137ddb65aa14100a51dc61b17e",0,"What is the
question?"],"value":1},
{"key":["d193f1137ddb65aa14100a51dc61b17e",1,"Choice 1"],"value":1}
]}

Would this suffice, for an all-in-one query?

Cheers,

Zach

On Sat, Jan 7, 2012 at 9:03 PM, Zachary Zolton <zachary.zolton@gmail.com> wrote:
> Well, if you know you're not going to keep adding properties, you can
> always set reduce_limit = false in your config.
>
> 2012/1/7 Tobi Schäfer <interface@p3k.org>:
>> thanks for your suggestion, zach!
>>
>> i am delighted to hear that issuing two requests is not despised.
>>
>> nevertheless, just for the sake of fully understanding map/reduce in couchdb, i read
your reply as although it might be a mess of code and counter-intuitive it is possible and
it would work… is that correct?
>>
>> best regards,
>> tobi
>>
>>
>> On 06.01.2012, at 18:57, Zachary Zolton wrote:
>>
>>> Tobi,
>>>
>>> You probably can somehow cram all that stuff together via the reduce
>>> function, but you miss out on being able to use the built in _count
>>> reduction and it'll take a mess of code.
>>>
>>> I think you're better off trying to retrieve this information in two
>>> queries: first query for the names and IDs of the polls and then do a
>>> second query for the count of choices of those poll IDs.
>>>
>>>
>>> Cheers,
>>>
>>> Zach
>>

Mime
View raw message