Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 60828 invoked from network); 26 Aug 2009 22:49:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 26 Aug 2009 22:49:22 -0000 Received: (qmail 16228 invoked by uid 500); 26 Aug 2009 22:49:21 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 16169 invoked by uid 500); 26 Aug 2009 22:49:21 -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 16159 invoked by uid 99); 26 Aug 2009 22:49:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Aug 2009 22:49:21 +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 kevin@meebo-inc.com designates 64.78.22.18 as permitted sender) Received: from [64.78.22.18] (HELO EXHUB017-3.exch017.msoutlookonline.net) (64.78.22.18) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Aug 2009 22:49:10 +0000 Received: from EXVMBX017-12.exch017.msoutlookonline.net ([64.78.22.53]) by EXHUB017-3.exch017.msoutlookonline.net ([64.78.22.18]) with mapi; Wed, 26 Aug 2009 15:48:48 -0700 From: Kevin Ferguson To: "user@couchdb.apache.org" Date: Wed, 26 Aug 2009 15:46:39 -0700 Subject: RE: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system Thread-Topic: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system Thread-Index: AcomnYC9p2ZcybC7QmGeUSF2o7lc/wAAZIVV Message-ID: References: <9ce8c9f70908261418s5807965ax2b9e23a0578dcff8@mail.gmail.com> In-Reply-To: <9ce8c9f70908261418s5807965ax2b9e23a0578dcff8@mail.gmail.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org If I understand you right, I would try this: map: emit(doc.score, 1) reduce: return sum(v); When you query on a particular key, you get the number of docs with that ex= act score. When you query on a range, you get the number of docs with scores in that r= ange. So to find the rank of e.g. score 25, you could query startkey=3D26&= endkey=3D99999999, which will return the number of docs that have 26 or hig= her. hth Kevin ________________________________________ From: larryroot@gmail.com [larryroot@gmail.com] On Behalf Of Larry [contact= @lroot.com] Sent: Wednesday, August 26, 2009 2:18 PM To: user@couchdb.apache.org Subject: Looking for help/ideas on how to fetch a Rank from a CouchDB based= Highscores system I currently have a a bunch of "high score" documents in couchdb. I'm trying to find the best way for me to create a view that would allow me to easily get the rank for a given score. As an example consider that I have 5 high scores: 1) John, 100 pts 2) Jane, 90 pts 3) Bill, 85 pts 4) Jen, 70 pts 5) Bob, 55 pts I have a function in my application called getRank( int $score) which would return the rank (int) for the given score. So for example, using the scores above: getRank(30) =3D=3D 6 getRank(71) =3D=3D 4 getRank(101) =3D=3D 1 My document structure for a highscore is: { "_id": "0a68cdbe4a8d130662217f3abf4046bc", "_rev": "1-1264504508", "score": "000000000000000000000000000000100", "player_name": "John" } My initial approach was to create a view that does emit([doc._id,doc.score]= , null). I Then query the view by setting my start key to the *top* of the view list and set my end key to the value of the score I'm looking for. I then count the results that are returned and derive the rank that way. This is grossly inefficient I know and I'm having a hard time trying to find the most optimal way to construct this. Any thoughts are ideas would be greatly appreciated. Thanks larry