Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EBB50BEAA for ; Fri, 6 Jan 2012 17:59:00 +0000 (UTC) Received: (qmail 33181 invoked by uid 500); 6 Jan 2012 17:58:59 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 32938 invoked by uid 500); 6 Jan 2012 17:58:58 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 32930 invoked by uid 99); 6 Jan 2012 17:58:58 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Jan 2012 17:58:58 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of zachary.zolton@gmail.com designates 209.85.160.52 as permitted sender) Received: from [209.85.160.52] (HELO mail-pw0-f52.google.com) (209.85.160.52) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Jan 2012 17:58:51 +0000 Received: by pbbb10 with SMTP id b10so1565834pbb.11 for ; Fri, 06 Jan 2012 09:58:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=1jxdlb7ql6t8qDfTFgHJRvLdJ3w2UA7hvJ3V4s6z7M8=; b=EpmbcScAIAxNaL0Gf2J/0alj/cOhneGn1nflwmDtFcgrNNLYXFwnhrzhPQQddJNYWX mDKqU4wY1nQ2w/ZJMsgvoqcygDEWgk/fQk76FwMlR9QTmJnqxLfUFF8tkyyuRYQQj7YH 8YvwTXkmI//J3Uk16LhHutdmxOMXJH8JZ7zEY= Received: by 10.68.73.106 with SMTP id k10mr17371902pbv.35.1325872710327; Fri, 06 Jan 2012 09:58:30 -0800 (PST) MIME-Version: 1.0 Received: by 10.143.69.19 with HTTP; Fri, 6 Jan 2012 09:57:59 -0800 (PST) In-Reply-To: <37E896DC-095B-4AAA-AA5C-67AAE7C3B46D@p3k.org> References: <37E896DC-095B-4AAA-AA5C-67AAE7C3B46D@p3k.org> From: Zachary Zolton Date: Fri, 6 Jan 2012 11:57:59 -0600 Message-ID: Subject: Re: Newbie question: How to collate sum() with other properties To: user@couchdb.apache.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 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 2012/1/6 Tobi Sch=E4fer : > Hello > > I am developing a little survey / poll tool as a first step in practicing= CouchDB. > > There are poll and vote documents stored in the database: > > poll =3D { > =A0 _id: 'd193f1137ddb65aa14100a51dc61b17e', > =A0 type: 'poll', > =A0 title: 'What is the question?', > =A0 choices: ['Choice 1', 'Choice 2, 'Choice 3'], > =A0 date: new Date > } > > vote =3D { > =A0 poll_id: 'd193f1137ddb65aa14100a51dc61b17e', > =A0 type: 'vote', > =A0 choice: 'Choice 1', > =A0 date: new Date > } > > Now I am trying to create a view of all polls together with their aggrega= ted votes for each choice as well as the total votes: > > map: function(doc) { > =A0 if (doc.type =3D=3D=3D 'poll') { > =A0 =A0 =A0emit([doc._id, doc.date], {title: doc.title}); > =A0 } else if (doc.type =3D=3D=3D 'vote' && doc.choice) { > =A0 =A0 =A0emit([doc.poll_id, doc.choice], {count: 1, title: doc.choice})= ; > =A0 =A0 =A0emit([doc.poll_id, '_total'], {count: 1, title: doc.choice}); > =A0 } > }, > > reduce: function(key, values) { > =A0 var i, value, result =3D {count: 0}; > =A0 for (i in values) { > =A0 =A0 =A0value =3D values[i]; > =A0 =A0 =A0result.title =3D value.title; > =A0 =A0 =A0if (value.count) { > =A0 =A0 =A0 =A0 result.count +=3D 1; > =A0 =A0 =A0} else { > =A0 =A0 =A0 =A0 delete result.count; > =A0 =A0 =A0} > =A0 } > =A0 return result; > } > > This seems to work but I am not sure if I will run into =93rereduce troub= le=94: > > ["d193f1137ddb65aa14100a51dc235509", "2012-01-05T15:36:40.632Z"] > {title: "What is the amazingly accurate answer to the ultimate question o= f life, the universe and everything?"} > ["d193f1137ddb65aa14100a51dc235509", "_total"] =A0 =A0 =A0 =A0 =A0{count:= 7, title: "6 =B7 9"} > ["d193f1137ddb65aa14100a51dc235509", "101010"] =A0{count: 2, title: "1010= 10"} > ["d193f1137ddb65aa14100a51dc235509", "42"] =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0{count: 3, title: "42"} > ["d193f1137ddb65aa14100a51dc235509", "6 =B7 9"] =A0 =A0 =A0 =A0 =A0 {coun= t: 1, title: "6 =B7 9"} > ["d193f1137ddb65aa14100a51dc235509", "XLII"] =A0 =A0 =A0 =A0 =A0 =A0{coun= t: 1, title: "XLII"} > > What do you think? Is there an easier or more convenient way to create su= ch a view? > > Best regards, > Tobi Sch=E4fer >