emit(doc.score, doc.player_name) The doc id is always available. This way you can sort by score, add a skip or a limit and everything should be fine. (you can even create a reduce method to get averages etc..)
- Matt On Wed, Aug 26, 2009 at 2:18 PM, Larry <[email protected]> wrote: > 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 >
