From user-return-14357-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Wed Dec 29 17:01:50 2010 Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 43562 invoked from network); 29 Dec 2010 17:01:50 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Dec 2010 17:01:50 -0000 Received: (qmail 65489 invoked by uid 500); 29 Dec 2010 17:01:48 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 65248 invoked by uid 500); 29 Dec 2010 17:01:45 -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 65240 invoked by uid 99); 29 Dec 2010 17:01:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Dec 2010 17:01:44 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=10.0 tests=MIME_QP_LONG_LINE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mike@cloudant.com designates 209.85.212.180 as permitted sender) Received: from [209.85.212.180] (HELO mail-px0-f180.google.com) (209.85.212.180) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Dec 2010 17:01:35 +0000 Received: by pxi17 with SMTP id 17so1063467pxi.11 for ; Wed, 29 Dec 2010 09:01:14 -0800 (PST) Received: by 10.142.157.7 with SMTP id f7mr12207399wfe.335.1293642073811; Wed, 29 Dec 2010 09:01:13 -0800 (PST) Received: from [10.0.0.5] (adsl-71-142-49-76.dsl.scrm01.pacbell.net [71.142.49.76]) by mx.google.com with ESMTPS id w42sm21110128wfh.3.2010.12.29.09.01.11 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 29 Dec 2010 09:01:12 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1082) Subject: Re: querying a view/reduce From: Michael Miller In-Reply-To: Date: Wed, 29 Dec 2010 09:01:07 -0800 Content-Transfer-Encoding: quoted-printable Message-Id: <22E0D727-AA70-45E0-A1FD-AEDEDA8ACA4F@cloudant.com> References: To: user@couchdb.apache.org X-Mailer: Apple Mail (2.1082) X-Virus-Checked: Checked by ClamAV on apache.org Not sure if you got a reply yet, but if you have keys like: > [2010, 11, 28, "fb45ffc66ad8f4f299f0bd06a3000903"] 10 <--- player 1 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3001c32"] 35 <--- player 2 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3000ccf"] 55 <--- player 3 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3000903"] 75 <--- player 1 = (again) Can't you just aggregate over users for a given date range via (bash = escaping): GET '...?startkey=3D\[2010,11,28\]&endkey=3D\[2010,11,24\]&group_level=3D3= ' Also, you'll get better performance by swapping your reduce function for = the simple builtin '_sum'. That is, just literally replace your js code = with '_sum'. -M On Dec 29, 2010, at 7:10 AM, Warner Onstine wrote: > Hi all, I've been trying to figure out the best way to implement this > particular view/reduce and have been having some issues. >=20 > Let me explain the structure a bit and what I've done. >=20 > I have a set of documents for storing a player's assignment completion > which contains a point value. I want to query for a given date range > for those completions and aggregate the points values. Here is the > view and the reduce I came up with : >=20 > map: > function(doc) { > if (doc.type =3D=3D 'playerassignmentcompletion' && doc.approved =3D=3D= true) { > var completionDate =3D new Date(doc.completionDate); > var completionYear =3D completionDate.getFullYear(); > var completionMonth =3D completionDate.getMonth(); > var completionDay =3D completionDate.getDate(); > emit([ > completionYear, > completionMonth, > completionDay, > doc.playerId > ], doc.pointsEarned); > } > } >=20 > reduce: > function(keys, values, rereduce) { > return sum(values); > } >=20 > And then I say group=3Dtrue >=20 > I didn't realize my issue at the time because all of the completions I > had were for a given date. Now, I added a completion for a different > date and what happens is this kind of result: > [2010, 11, 28, "fb45ffc66ad8f4f299f0bd06a3000903"] 10 <--- player 1 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3001c32"] 35 <--- player 2 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3000ccf"] 55 <--- player 3 > [2010, 11, 26, "fb45ffc66ad8f4f299f0bd06a3000903"] 75 <--- player 1 = (again) >=20 > What I want is to combine all the player's total scores, but with my > group=3Dtrue, it is matching each of my keys exactly. I understand why > it's doing it, just not how to fix my key/query to work the way I want > it to. >=20 > One thought I had was to move the id column up to the front so that I > could do group_level=3D1. But I don't know how to specify a wildcard > parameter so that I get all the documents with any key and just the > particular date range I want. >=20 > Hopefully this is clear enough. I can share more information if it's > necessary (just don't want to bog the list down with too many > attachments). >=20 > Thanks, it is greatly appreciated! >=20 > -warner