大型收藏的排行榜排名 [英] Leaderboard rankings on large collections

查看:69
本文介绍了大型收藏的排行榜排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:

我们在 MongoDB 集合 playerProfiles 中拥有 5 亿视频游戏玩家.每个文档都有多个属性,例如 winskills.现在我们想为这些属性创建一个排行榜,以便我们可以计算出每个玩家在 winskills 中的排名.

We've got 500 million players of a videogame in a MongoDB collection playerProfiles. Each document has several properties like wins or kills. Now we'd like to create a leaderboard for these properties so that we can figure out each player's rank for wins or kills.

对于 Top 100 排行榜,我只需按这些属性之一对集合进行排序,然后将结果缓存在 redis 中,例如 5 分钟.我正在努力解决的是如何使所有玩家资料都能做到这一点.

For the Top 100 leaderboard I would simply sort the collection by one of these properties and cache the result for let's say 5 minutes in redis. What I am struggling with is how I can make this work for all player profiles.

问题:

我应该如何获得每个玩家的 winskills 排名,以免数据库服务器不堪重负?如果有道理,我可以随意使用其他数据库系统来解决这个问题.

How should I get every player's rankings for wins or kills so that it doesn't overwhelm the database server? If it makes sense I am free to use other database systems to solve this issue.

我的想法:

我的第一个想法是创建一个自己的 mongodb 集合,它只包含玩家的 id 和 killswins 的排名.然后我会定期更新这个集合.但即使更新这个 5 亿玩家档案的集合也需要花费大量时间.有没有更好的方法来解决这类问题?

My first idea was to create an own mongodb collection which only contains the player's id and it's rankings for kills and wins. I would then regularly update this collection. But even updating this collection of 500m player profiles should take a significant time. Is there a better approach for this type of problem?

推荐答案

你可以使用 mongodb 聚合来实现这一点.你基本上按 wins 和 kills 对文档进行排序,然后在保留索引的同时将每个用户推送到一个新数组中,这index 是用户的排名之后,您可以简单地在这个新创建的数组上使用 'unwind' 并找到有问题的用户,这将为您提供用户的排名和其他详细信息.

you could use mongodb aggregate to achieve this.You basically sort the documents by wins and kills and then push each user in a new array while preserving the index, this index is the users' rank after that you can simply use 'unwind' on this newly created array and find the user in question, this will give you the user's rank with other details.

这是一个示例聚合管道:

Here is a sample aggregate pipeline:

[{
    "$sort": { 
        "wins": -1
    }
},
{
    "$group": {
        "_id": false,
        "players": {
            "$push": {
                "_id": "$_id",
                "playerId": "$playerId",
                "wins": "$wins"
            }
        }
    }
},
{
    "$unwind": {
        "path": "$players",
        "includeArrayIndex": "rank"
    }
},
{
    "$match": {
        "players._id": 1234567890
    }
}]

现在您应该有一个包含玩家详细信息的等级字段,它会根据获胜次数告诉您玩家的等级,您可以扩展并添加击杀数.

Now you should have a rank field with the player details which will tell you the rank of the player based on wins, you can extend and add kills to the sort.

希望这有帮助!:)

这篇关于大型收藏的排行榜排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆