Firebase排行榜,最佳实践。 SQL联接和orderby [英] Firebase Leaderboard, best Implmentation. SQL Joins and orderby

查看:204
本文介绍了Firebase排行榜,最佳实践。 SQL联接和orderby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用Firebase作为数据库的排行榜应用程序。我来自一个老派的SQL背景,所以这个新的noSQL数据库类型给了我数据库密钥和参考焦虑。



如果我有组,每个组都有成员,那么所有的用户都有各种测试的分数,那么获得给定测试分数的分组数据的最有效方式是什么?在SQL中,这显然是微不足道的使用连接,然后orderby,但迄今为止在firebase文档中,我无法锻炼如何有效地查询获取连接的数据块,而无需在客户端上进行一些排序工作。


$ b

我现在的解决方案如下,但是在客户端做所有的排序似乎很痛苦。我目前得到了该组的所有用户,然后迭代获取每个用户的测试分数的单独记录。无论日期如何,我都需要获得给定测试的最高分。

  var testName ='test1'; 
var scores = firebase.database()。ref('/ groupMembers /'+'TestGroup')。once('value')。then(function(snapshot){
console.log(snapshot。 val());
(在snapshot.val()中的var用户){
//...查询每个单独用户在test1上的得分数据库
}
/ /在客户端js
})对测试成绩进行排序;

数据库设计的任何提示都不会有问题!

  groupMembers {
TestGroup {
user1:true
user2:true
}
}

testScores {
user1 {
test1 {
date:11/04/2016
score:90
}
test1 {
日期:05/07/2016
得分:100
}
测试2 {
日期:11/06/2016
得分:50
}
}
user2 {
test1 {
date:15/04/2016
score:70
}
test2 {
日期:05/07/2016
得分:80
}
测试2 {
日期:11/10/2016
得分:50
}


code


谢谢

解决方案

我想你肯定会走正确的道路。对不起,有一点改变你的数据,不知道你怎么能有两个同名的键(test1在第一个用户和test2在第二),但我会像这样存储你的数据:

  {
groupMembers:{
TestGroup:{
tests:{
test1:true,
test2:true
},
users:{
user1:true
user2:true
}
}
},
userTestScores:{
user1:{
test1:true,
test2:true
},
user2:{
test1:true,
test2:true
}
},
testScores:{
test1:{
users:{
user1:{
date:11 / 04/2016
得分:90

user2 :{
date:15/04/2016
得分:70
}
}
},
test2:{
个用户:{
user1:{
date:11/06/2016
score:50
},
user2:{
date:05 / 07/2016
得分:80
}
}
}
}
}
pre>

然后您的查询将会在测试组中找到所有测试,然后您可以轻松找到最佳分数

  firebase.database()。ref('/ groupMembers / TestGroup / tests')。once('value')。then(function(snapshot){
对于(snapshot.val()中的var test){
firebase.database()。ref('/ groupMembers / testScores / tests /'+ test +'/users/').orderByChild('score')。 limitToLa (1).once('value')。然后(函数(快照){
//最高分将在这里
});
}
});


I am creating a leaderboard app using Firebase as the database. I come from an old school SQL background so this new noSQL type of databasing is giving me database key and reference anxiety.

If I have groups, each groups having members, and then all the users have scores for various tests, what is the most efficient way of getting the data for a group where the scores of a given test are displayed in order. In SQL this is obviously trivial using joins and then orderby but so far in the firebase docs I can't workout how to efficiently query to get blocks of joined data without having some of the sorting work on the client side

my current solution is, given below but it seems painful to have to do all the sorting on the client side. I currently get all the users for the group, and then iterate to get the individual records of test scores for each user. I need to get the single highest score for a given test, regardless of date.

var testName = 'test1';
var scores = firebase.database().ref('/groupMembers/' + 'TestGroup').once('value').then(function(snapshot) {
                console.log(snapshot.val());
                for(var user in snapshot.val()){
                    //...query the database for each separate users score on test1
                }
            //sort the test scores here on client side js
            });

Any tips of the database design won't go amiss either!

groupMembers{
   TestGroup{
       user1 : true
       user2 : true
   }
}

testScores{
   user1{
      test1{
         date : 11/04/2016
         score : 90
      }
     test1{
         date : 05/07/2016
         score : 100
      }
     test2{
         date : 11/06/2016
         score : 50
      }
   }
   user2{
      test1{
         date : 15/04/2016
         score : 70
      }
     test2{
         date : 05/07/2016
         score : 80
      }
     test2{
         date : 11/10/2016
         score : 50
      }
   }
}

Thanks

解决方案

I think your definitely going down the right path. Sorry for changing your data around a bit, not sure how you can have two keys with the same name (test1 in the first user and test2 in the second) but I would store your data like this:

{
    groupMembers : {
        TestGroup : {
            tests : {
                test1 : true,
                test2 : true
            },
            users : {
                user1 : true
                user2 : true
            }
        }
    },
    userTestScores : {
        user1 : {
            test1 : true,
            test2 : true
        },
        user2 : {
            test1 : true,
            test2 : true
        }
    },
    testScores : {
        test1 : {
            users : { 
                user1: {
                    date : "11/04/2016"
                    score : 90
                },
                user2: {
                    date : "15/04/2016"
                    score : 70                  
                }
            }
        },
        test2: {
            users: {
                user1: {
                    date : "11/06/2016"
                    score : 50
                },
                user2: {
                    date : "05/07/2016"
                    score : 80              
                }
            }
        }
    }
}

And then your query would be to find all the tests in a test group and then you can easily find the best score

firebase.database().ref('/groupMembers/TestGroup/tests').once('value').then(function (snapshot) {
    for (var test in snapshot.val()) {
        firebase.database().ref('/groupMembers/testScores/tests/' + test + '/users/').orderByChild('score').limitToLast(1).once('value').then(function (snapshot) {
            //highest score will be here
        });
    }
});

这篇关于Firebase排行榜,最佳实践。 SQL联接和orderby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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