Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 9282 invoked from network); 15 Dec 2009 23:37:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Dec 2009 23:37:31 -0000 Received: (qmail 23304 invoked by uid 500); 15 Dec 2009 23:37:30 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 23246 invoked by uid 500); 15 Dec 2009 23:37:30 -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 23236 invoked by uid 99); 15 Dec 2009 23:37:30 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Dec 2009 23:37:30 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of pawelstawicki@gmail.com designates 209.85.219.216 as permitted sender) Received: from [209.85.219.216] (HELO mail-ew0-f216.google.com) (209.85.219.216) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Dec 2009 23:37:21 +0000 Received: by ewy8 with SMTP id 8so447389ewy.35 for ; Tue, 15 Dec 2009 15:37:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=HHd1t2xK5q6m93pWp3WwOwhdOQHij/KG7rfqsiqhqlc=; b=d0q2aBG7Uv4v7NJIylQpCSf7hP038889xLJKuR5LIIs1gt9ap3FsVOWwabssBDhXuJ HwXphBYBe+xv/yzdn3daYFiV1MGbTPgvU2w+LiUyxwUVS0vvKeNX9EfkmU9j0wXfij36 BfY/oOpV/eamlIvhcInUTU5wTLOp6Yla9R1/I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=iWfgGYl+wlm2XHxvb/HmxKarYXPZDIqB7lIzNuVtYkKv1OK2sq1AbnQCbKbtbo3hRB R2x02hAXA4G41NYN7cSOV7um+50NdczpPEcIoD/youaMPuxAVpMvXlf/B6KDGccIcTJm h4JuYrGIUUsgPIbNwc+zO1GjzB3ySwZjZosII= MIME-Version: 1.0 Received: by 10.213.96.202 with SMTP id i10mr5838167ebn.99.1260920220881; Tue, 15 Dec 2009 15:37:00 -0800 (PST) In-Reply-To: <1bca98390912150640m597009e5m78d29461d92a6ae0@mail.gmail.com> References: <1bca98390912150640m597009e5m78d29461d92a6ae0@mail.gmail.com> From: =?UTF-8?Q?Pawe=C5=82_Stawicki?= Date: Wed, 16 Dec 2009 00:36:39 +0100 Message-ID: <6adfa88d0912151536v5c70834hfdb3caea9e238253@mail.gmail.com> Subject: Re: returning document list and a count of related documents To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org Hmm... Maybe try to have document as a key? // map.js function(doc) { =C2=A0if (doc.type =3D=3D "post") { =C2=A0 =C2=A0map(doc, 0); =C2=A0} else if (doc.type =3D=3D "comment") { =C2=A0 =C2=A0map({'_id':doc.post}, 1); =C2=A0} } // reduce.js function(keys, values, rereduce) { =C2=A0 =C2=A0 =C2=A0 =C2=A0return sum(values); } Then call view with "group=3Dtrue" parameter. You should get document as key, and number of comments as value. Another solution, if you need post id as a key and post document as value: // map.js function(doc) { =C2=A0if (doc.type =3D=3D "post") { =C2=A0 =C2=A0map(doc._id, [doc, 0]); =C2=A0} else if (doc.type =3D=3D "comment") { =C2=A0 =C2=A0map(doc.post, [{'_id':doc.post}, 1]); =C2=A0} } // reduce.js function(keys, values, rereduce) { if (!rereduce) { var commentsCount =3D 0; values.forEach(function(array) { commentsCount +=3D array[1]; } return [values[0][0], commentsCount; } else { var commentsCount =3D 0; values.forEach(function(array) { commentsCount +=3D array[1]; } return [values[0], commentsCount; } } WARNING: will work only with "group=3Dtrue" parameter. I am not sure it will work, but it definitely won't work with group_level =3D 0 (which is default), so use group=3Dtrue. It should give you post id as a key, and post document and comments count as value. Regards --=20 Pawe=C5=82 Stawicki http://pawelstawicki.blogspot.com http://szczecin.jug.pl http://www.java4people.com