couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zachary Zolton <zachary.zol...@gmail.com>
Subject Re: View collation by one key and sorting by another key
Date Fri, 05 Aug 2011 20:23:49 GMT
Matthew,

The short answer is no. CouchDB views are collated by the emitted key
and cannot be sorted or filtered in multiple dimensions.

I would solve this problem by querying the range of player documents
whose lastDateActive falls within the past week. Then find the top 10
players, by numberOfKills, in either the client code or in a _list
function.

You do need to consider how many players are likely to have been
active in the past week though, since the secondary filtering will
take more time as the number of docs resulting from the range query
increases. So, this approach doesn't scale indefinitely, though may be
good enough until your game becomes massively popular... (^_^

Hope that makes sense!


Cheers,

Zach


On Fri, Aug 5, 2011 at 9:27 AM, Matthew Slade
<matthew.slade@moonlight42.com> wrote:
> Hi All
>
> I've been struggling with creating a view for my game's weekly leaderboard.
> For simplicities sake lets assume my player document is structured in
> the following way:
>
> {
> "_id":"myuserid",
> "lastDateActive": "2011-07-06T10:24:52",
> "numberOfKills": 50
> }
>
> If I create a map function with the "lastDateActive" as the key I can
> use a start and an end key to collate the data and to only count users
> that fall within the last week.
>
> If I create a map function with the "numberOfKills" as the key the
> data is automatically ordered for me by kills meaning I can specify a
> limit of 10 in the options and successfully pull out only the top 10
> results.
>
> But If I create a complex key with either of these fields as the first
> key I am unable to collate or order the data successfully since the
> ordering can only satisfy one of the above conditions not both.
>
> Is there anyway that one could successfully merge these two
> requirements and perform this sort of operation where data is collated
> by 1 key and sorted by another?
>
>
> Thank you in advance for your time and help
>
> Matthew
>

Mime
View raw message