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 exact score. When you query on a range, you get the number of docs with scores in that range. So to find the rank of e.g. score 25, you could query startkey=26&endkey=99999999, which will return the number of docs that have 26 or higher. hth Kevin ________________________________________ From: [email protected] [[email protected]] On Behalf Of Larry [[email protected]] Sent: Wednesday, August 26, 2009 2:18 PM To: [email protected] 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) == 6 getRank(71) == 4 getRank(101) == 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
