incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ryszard Szopa" <ryszard.sz...@gmail.com>
Subject how to do it in couchDB
Date Tue, 10 Jun 2008 07:24:41 GMT
Let's say I have a database with objects that may look either like

{type: "user", _id: "foobar", team: "the_foobars"}

or like

{type: "score", user: "foobar", value: 5}.

So, there are some users that belong to teams, and there are their
scores, in which the attribute "user" is a reference to th person that
has achieved the score.

It is very easy to fetch a user and his results using view collation
as proposed in [1] by passing the following view a key argument with
the user id:

function(doc) {if (doc.type == "user") {emit([doc._id,1], doc);} else
if (doc.type == "score") {emit([doc.user,2], doc.value);}}

We can then use a reduce function (which should ignore the first
document, which is a user) to get the mean of the results achieved by
the user.

It is also easy to fetch all the users in a team (just make doc.team
the key if doc.type is "user"). What should I do, however, if I want
to have the mean score of a team?

I was thinking of two approaches.

a) Scores could have a team attribute.

b) Write a complex reduce function which would return what I want.

(a) doesn't seem good because it introduces too much denormalization
to my taste (also, if then a user changes his team, you have to change
all his scores). (b) neither seems good because it isn't much better
than just doing the right calculation on the client side (well, maybe
except caching).

The most natural way of solving this problem would be to first have a
view with a map emitting users and their scores, keyed by user_id, and
a reduce that groups a user and his results in one object, and *then*
run a query on this, mapping teams-names to lists of results and
reduce it to get the mean. Unfortunately, I have no idea if something
like this is possible in CouchDB.

Is there some way of doing something like this? Is the possibility of
doing something like this planned as a future functionality? Or (this
is the answer I would be most happy about ;)) there is some other,
natural way of achieving the same result I just missed?

(My hopes are somehow boosted by the planned grouped reduce functionality).

Cheers,

    -- Richard

[1] http://www.cmlenz.net/archives/2007/10/couchdb-joins
-- 
http://szopa.tasak.gda.pl/

Mime
View raw message