Firebase排行榜,最佳执行.SQL连接和顺序
Firebase Leaderboard, best Implmentation. SQL Joins and orderby
我正在创建一个使用Firebase作为数据库的排行榜应用程序。我来自一个传统的SQL背景,所以这种新的noSQL类型的数据库给了我数据库键和引用的焦虑。
如果我有组,每个组都有成员,然后所有用户都有各种测试的分数,那么获取给定测试分数按顺序显示的组的数据的最有效方法是什么?在SQL中,这显然是微不足道的,使用连接,然后排序,但到目前为止,在firebase文档中,我无法锻炼如何有效地查询,以获得连接数据块,而不需要在客户端进行一些排序工作
我目前的解决方案如下所示,但必须在客户端进行所有排序似乎很痛苦。我目前获得该组的所有用户,然后迭代获得每个用户的测试分数的单独记录。我需要在给定的测试中获得单一最高分,而不管日期。
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
});
数据库设计的任何技巧也不会出错!
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
}
}
}
谢谢
在NoSQL中,您经常按照您想要访问的方式对数据进行建模。我强烈推荐阅读这篇关于NoSQL数据建模的文章。
如果我对你的问题理解正确的话,你想要显示一个包含该组得分最高的用户的特定组的排行榜。对于Firebase查询,您可以使用以下数据模型:
groupScores: {
TestGroup: {
user1: { // the highest score for user1
test: "test1"
date : 05/07/2016
score : 100
}
user2: { // the highest score for user2
test: "test2"
date : 05/07/2016
score : 80
}
}
}
所以你不会知道我只保留这个模型中每个用户的最高分,不管用户在什么测试中得到这个分数。这样每个用户只能在排行榜中出现一次。如果您的用例不同,则必须以不同的方式对数据建模。
有了这个数据结构,你可以得到排行榜:
var query = ref.child('groupScores/TestGroup').orderByChild('score').limitToLast(10);
query.on('value', function(snapshot) {
// TODO: loop over children, invert them, show leaderboard
});
我认为你的选择是正确的。对不起,改变你的数据周围一点,不知道你怎么能有两个键具有相同的名称(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"
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
}
}
}
}
}
然后你的查询将是找到测试组中的所有测试然后你可以很容易地找到最好的分数
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
});
}
});
相关文章:
- ajax请求的顺序总是不同的
- 按照选项卡索引的顺序循环一个jQuery选择
- 匹配一个单词,其中候选人可以跨越顺序组(跨度)
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 按顺序添加和删除类
- 如何使用密码检测网络中的状态连接
- 如何在javascript中按正确的顺序连接波斯语字符和英语字符
- 按顺序连接两个数组的成员
- 保存动态创建的 jQuery 连接可排序列表的顺序
- 我可以't在javascript中添加两个变量.如果我更改要添加的变量的顺序,它要么忽略第二个变量,要么连接
- 使用Gulp按正确顺序连接js文件
- 如何从远程服务器下载一堆文件,并使用Node.js按特定顺序连接它们
- 编译后按一定顺序连接typescript文件
- 使用ANT连接JS文件(保持特定顺序)
- Javascript优化:什么工具可以压缩顺序字符串连接?
- Firebase排行榜,最佳执行.SQL连接和顺序
- Gulp顺序-如何最后连接或添加某个javascript文件
- Typescript:生成文件中的连接顺序
- 最小化和连接的顺序对JavaScript/CSS文件的批量处理有影响吗?
- Javascript字符串以错误的顺序连接