incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Miller <m...@cloudant.com>
Subject Re: querying a view/reduce
Date Wed, 29 Dec 2010 17:01:07 GMT
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=\[2010,11,28\]&endkey=\[2010,11,24\]&group_level=3'

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.
> 
> Let me explain the structure a bit and what I've done.
> 
> 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 :
> 
> map:
> function(doc) {
>    if (doc.type == 'playerassignmentcompletion' && doc.approved == true) {
>        var completionDate = new Date(doc.completionDate);
>        var completionYear = completionDate.getFullYear();
>        var completionMonth = completionDate.getMonth();
>        var completionDay = completionDate.getDate();
>        emit([
>            completionYear,
>            completionMonth,
> 	        completionDay,
>            doc.playerId
>        ], doc.pointsEarned);
>    }
> }
> 
> reduce:
> function(keys, values, rereduce) {
>    return sum(values);
> }
> 
> And then I say group=true
> 
> 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)
> 
> What I want is to combine all the player's total scores, but with my
> group=true, 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.
> 
> One thought I had was to move the id column up to the front so that I
> could do group_level=1. 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.
> 
> 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).
> 
> Thanks, it is greatly appreciated!
> 
> -warner


Mime
View raw message