Firebase实时数据库:按值排序后,如何在特定键周围检索10条记录? [英] Firebase realtime database: how can I retrieve 10 records around a specific key after ordered by value?

查看:88
本文介绍了Firebase实时数据库:按值排序后,如何在特定键周围检索10条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Unity在Firebase实时数据库中创建了一个简单的排行榜:

I created a simple leaderboard in my Firebase Realtime Database, using Unity:

键对应于用户名,值对应于用户分数.

Keys correspond to the user name and the values correspond to the user score.

我想

  1. 检索前20条记录
  2. 按分数排序时,检索给定用户名(上2个,下2个)周围的5条记录.

通过以下查询,我设法检索了前100名得分:

I manage to retrieve top 100 scores by the following query:

FirebaseDatabase.DefaultInstance.GetReference (LEADERBOARD_TABLE_CAPTION).Child (leaderboardName).OrderByValue ().LimitToLast (20)

我使用LimitToLast是因为排序是按升序进行的.

I use LimitToLast because the ordering is done in ascending order.

结果如下:

1.test31,987
2.test85,975
3.test67,943
4.test94,941
5.test95,940
6.test71,929
7.test16,917
8.test64,911
9.test100,906
10.test42,870
11.test54,863
12.test88,861
13.test5,859
14.test97,844
15.test46,836
16.test26,765
17.test52,764
18.test4,763
19.test70,756
20.test51,742

然后在第二项任务中,我想检索用户名附近的分数,比如说"test100".我想检索这样的结果(请注意,test100不必排在前20位):

Then for the second task, I want to retrieve scores around a user name, let say "test100". I want to retrieve such a result (note that test100 does not need to be in top 20):

 7.test16,917
 8.test64,911
 9.test100,906
 10.test42,870
 11.test54,863

我想运行这样的查询,该查询以test100开始,限制为前3个,然后以test100限制结束,最后3个,但以按值排序的方式进行.没有这样的功能(我无法找到). StartAt和EndAt始终基于,但是我无法基于分数906(可能有很多用户的分数为906)居中查询.

I want to run such a query that start at test100 and limit to first 3, then end at test100 limit to last 3, but in ordered by value fashion. There is no such function (I could not manage to find). StartAt and EndAt are always based on the value , but I cannot center my query based on the score 906 (there may be lots of user have score 906).

即使只有906个,我的解决方案也不起作用.当我居中并获得大约906的前2个分数和后2个分数时,我不知道居中值的排名,也无法显示当前用户的当前排名是多少.最无效的解决方案是检索按值排序的所有分数并搜索当前用户,但是当我有10.000条记录时,这不是解决方案.

Even if there is only one 906 still my solution does not work. When I center and get first 2 and last 2 scores around 906, I do not know the ranking of the centering value and I cannot show what is the current ranking of my current user. The most inefficient solution is to retrieve all the scores ordered by value and search for the current user but it is not a solution when I have 10.000 records.

我该怎么做?

最诚挚的问候,

fercis

推荐答案

Firebase数据库查询可以检索从起始于特定键/值的项或从终止于特定项的项核心价值.要检索特定键之前的5个项目和特定键之后的5个项目,您将需要使用两个查询.

Firebase Database queries can retrieve items starting at a specific key/value or items ending at a specific key/value. To retrieve the 5 items before a specific key and the five items after a specific key, you will need to use two queries.

leaderboard = FirebaseDatabase.DefaultInstance.GetReference(LEADERBOARD_TABLE_CAPTION).Child(leaderboardName);
var anchorItemKey = "...";
Query itemsBefore = leaderboard.orderByKey().endAt(anchorItemKey).limitToLast(6);
Query itemsAfter = leaderboard.orderByKey().startAt(anchorItemKey).limitToFirst(6);

上面的每个查询将检索(最多)6个项目,这比您需要的多一倍.这样做的原因是查询还将检索锚点项本身.

Each query above will retrieve (up to) 6 items, which is one more than you need. The reason for this is that the query will also retrieve the anchor item itself.

更新:如果要基于得分检索项目,请在特定键周围使用startAt()方法和两个参数:

Update: If you want to retrieve the items based on score, around a specific key, you'd use the startAt() method with two parameters:

leaderboard = FirebaseDatabase.DefaultInstance.GetReference(LEADERBOARD_TABLE_CAPTION).Child(leaderboardName);
var anchorItemKey = "...";
var scoreOfAnchorItem = ...;
Query itemsBefore = leaderboard.orderByChild("score").endAt(scoreOfAnchorItem, anchorItemKey).limitToLast(6);
Query itemsAfter = leaderboard.orderByChild("score").startAt(scoreOfAnchorItem, anchorItemKey).limitToFirst(6);

使用此变体,Firebase将按分数对项目进行排序,然后在定位项目的键周围返回项目.

With this variant, Firebase will order the items by score and then return the items around the anchor item's key.

这篇关于Firebase实时数据库:按值排序后,如何在特定键周围检索10条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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