问题
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.
I want to
- Retrieve first 20 records
- Retrieve the 5 records around a given user name (upper 2, itself and the lower 2) when ordered by the scores.
I manage to retrieve top 100 scores by the following query:
FirebaseDatabase.DefaultInstance.GetReference (LEADERBOARD_TABLE_CAPTION).Child (leaderboardName).OrderByValue ().LimitToLast (20)
I use LimitToLast because the ordering is done in ascending order.
The result looks like:
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
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
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).
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.
How can I do this?
Best regards,
fercis
回答1:
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);
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.
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);
With this variant, Firebase will order the items by score and then return the items around the anchor item's key.
来源:https://stackoverflow.com/questions/44095883/firebase-realtime-database-how-can-i-retrieve-10-records-around-a-specific-key