简体   繁体   English

CouchDB排名

[英]CouchDB Ranking

I'm trying to figure out the best way of sorting by rank using couch db. 我试图找出使用Couch db按等级排序的最佳方法。 I have my documents setup in a players db like so: 我将文件设置在播放器数据库中,如下所示:

{
   "_id": "user2",
   "_rev": "31-65e0e5bb1eba8d6a882aad29b63615a7",
   "username": "testName",
   "apps": {
       "app1": {
           "score": 1000
       },
       "app2": {
           "score": 1000
       },
       "app3": {
           "score": 1000
       }
   }
}

The player can have multiple scores for various apps. 播放器可以为各种应用程序提供多个分数。 I'm trying to figure out the best way to pull say the top 50 scores for app1. 我正在尝试找出最好的方法来吸引app1的前50名得分。

I think one idea could be to store the score of each user for each app seperately. 我认为一个想法可能是分别为每个应用程序存储每个用户的分数。 Like so: - 像这样:-

{"app":"app1","user":"user_id","score":"9000"}

The you could write a map function 你可以写一个地图函数

emit([doc.app,doc.score],{_id:doc.user,score:doc.score})

and query the view like 然后像这样查询视图

/view_name?include_docs=true&startkey=["app1",{}]&endkey=["app1"]&descending=true

With this arrangement you have a view sorted by the score and the name of the app. 通过这种安排,您可以按得分和应用程序名称对视图进行排序。 Here are the results that you get. 这是您得到的结果。

{"total_rows":2,"offset":0,"rows":[

{"id":"61181c784df9e2db5cbb7837903b63f5","key":["app1",10000],"value":

{"_id":"5002539daa85a05d3fab16158a7861c1","score":10000},"doc": {"_id":"5002539daa85a05d3fab16158a7861c1","_rev":"1-8441f2f5dbaaf22add8969cea5d83e1b","user":"jiwan"}} , {"_id":"5002539daa85a05d3fab16158a7861c1","score":10000},"doc": {"_id":"5002539daa85a05d3fab16158a7861c1","_rev":"1-8441f2f5dbaaf22add8969cea5d83e1b","user":"jiwan"}}

{"id":"7f5d53b2da8ae3bea8e2b7ec74020526","key":["app1",9000],"value":

{"_id":"336c2619b052b04992947976095f56b0","score":9000},"doc": {"_id":"336c2619b052b04992947976095f56b0","_rev":"3-3e4121c1831d7ecafc056e71a2502f3a","user":"akshat"}} ]} {"_id":"336c2619b052b04992947976095f56b0","score":9000},"doc": {"_id":"336c2619b052b04992947976095f56b0","_rev":"3-3e4121c1831d7ecafc056e71a2502f3a","user":"akshat"}} ]}

You have score in value. 您的价值得分。 User in doc. 文件使用者。

Edit 编辑

Oops! 哎呀! I mistyped the startkey and endkey :) Notice that it is not startKey but startkey same for endkey . 我输入了错误的startkeyendkey :)注意,它不是startKeystartkey同为endkey Also note that since descending is true we reverse the order of keys. 另请注意,由于降序为true,因此我们会反转键的顺序。 It should work as expected now. 现在应该可以正常工作了。

For more help check out 如需更多帮助,请查看

  1. This answer and 这个答案
  2. This answer 这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM