The leaderboard view is designed to generate
a list of the players and their current score:
function (doc) { if (doc.jsonType == "player") { emit(doc.experience, null); } }
The view looks for records with a jsonType of
"player", and then outputs the experience
field of each player record. Because the output from views is
naturally sorted by the key value, the output of the view will
be a sorted list of the players by their score. For example:
{ "total_rows" : 81, "rows" : [ { "value" : null, "id" : "Bob0", "key" : 1 }, { "value" : null, "id" : "Dustin2", "key" : 1 }, … { "value" : null, "id" : "Frank0", "key" : 26 } ] }
To get the top 10 highest scores (and ergo players), you can
send a request that reverses the sort order (by using
descending=true, for example:
http://127.0.0.1:8092/gamesim-sample/_design/dev_players/_view/leaderboard?descending=true&connection_timeout=60000&limit=10&skip=0Which generates the following:
{ "total_rows" : 81, "rows" : [ { "value" : null, "id" : "Tony0", "key" : 23308 }, { "value" : null, "id" : "Sharon0", "key" : 20241 }, { "value" : null, "id" : "Damien0", "key" : 20190 }, … { "value" : null, "id" : "Srini0", "key" :9 }, { "value" : null, "id" : "Aliaksey1", "key" : 17263 } ] }