How to get highest 10 scores with RestClient Unity from Firebase Realtime Database - firebase

I want to retrieve highest 10 scores from firebase . When i search stackoverflow i found some answers on here Firebase retrieve highest 100 score but i can not imagine how to implement them with REST API . My code is like this. I send request to end point and get response .
RestClient.Get<User>(databaseURL + getScoreText.text + ".json").Then(response =>
{
user = response;
UpdateScore();
});
Also for 2nd way, i can get all users' info by some json parser package and parse them and get the highest scores . But if my database gets bigger , i think i ll have problems . I need a way like "orderby". Do you know any way to implement "orderby" method to my restclient code ?

Reading the firebase docs:
Filtered data is returned unordered: When using the REST API, the filtered results are returned in an undefined order since JSON interpreters don't enforce any ordering. If the order of your data is important you should sort the results in your application after they are returned from Firebase.
It seems like scaling is a real limitation of the realtime database.
Firestore can do this pretty easily if you want to switch.
If you're sticking with realtime DB, I would save a separate database-reference that only ever contains the top 10 scores.
So when a player gets a new score, they would only upload it to this reference if it's higher than the 10th top score. This would mean you would only have a DB reference containing 10 or less top scores, and scaling wouldn't be a problem. I'm not very familiar with realtime DB, but I'm sure you could write a security to rule to enforce this in a secure, server authoritative manner, at the very least a cloud function.

Related

How to retrieve child nodes data in firebase by php

How to retrieve child nodes data and update values in firebase:
As I want to make an API in which I create parameters to pass values by it.
Like example I have one node in firebase name as users and under it has user amount is 10. I want to get that data of amount from users node and add some value with it like 10+5 will update in database as 15 in my firebase.
I tried to retrieve data from firebase by seeing youtube, but I'm unable to get what I want; I was only able to get the whole users node data at whole.
That sounds like a 2-step process:
Store the value as a number rather than as a string like you now have it.
Then use the atomic increment operation to increment it by 5.

Firebase firestore collection count with angularFire 2

I want to get the total number of the documents that exist in firestore.
I don't want to get the data only the total number of inside Products collection I have 200.000 items is that possible with Angular 4-5, not angular.js
Can someone expert tell me how I can achieve that ??
My code so far and is not work
get_total_messages() {
this.messages_collection = this.afs.collection<MessageEntity>('messages');
return this.messages_collection.snapshotChanges();
}
End this is how I try to get the data but is not what I want;
this.firebase_Service.get_total_messages().subscribe( data => {
console.log(data);
});
There is no API to get the count of the number of documents in a Firestore collection. This means that the only ways to get the count are:
Get all documents and count them client-side.
Store the count as a separate property and update that as you add/remove documents.
Both approaches are quite common in NoSQL databases, with the second of course being a lot more efficient as the number of documents grows.
Firebase provides a sample of using Cloud Functions to keep a counter. While this sample is written for the Firebase Realtime Database, it can easily be modified to work on Cloud Firestore too.
Firestore also provides documentation on running aggregation queries and running distributed counters. Both seem slightly more involved than the first sample I linked though.
this.firebase_Service.get_total_messages().subscribe( data=>this.totalnumber=data.length);
//now, you can get total number of messages
luckily , i've solved somehow using the code,
try this, and it works well .
this.db.collection('User').valueChanges()
.subscribe( result => {
console.log(result.length);
})

Firebase Database Unity differentiating between old and new data

I am building a chat engine using firebase in unity. I want to differentiate between the existing data and all the new data that gets added into the database. There is method once in web sdk of firebase which helps in differentiating between old and new data, is anyone aware if we have something similar on unity
There is no direct way to do this, one way that is a workaround is to add a timestamp value in all the entries maintained in the database and each time one subscribes for the new data we make use of the OrderByValue|OrderByKey and StartAt to do the same.
In the beginning value for StartAt will be 0 but post that whenever a child gets added we can update the StartAt value to that so that the next time client subscribes for the childAdded, it will only receive data post the last child.

Firebase database: High number of calls with minimal data

I built my Firebase database as flat as possible.
This is my structure: https://stackoverflow.com/a/40115527/3669981
with a little adjustment: Instead of role values in my projectsRoles node I'm using the role keys assigned to the /roles node (So I can add and edit roles easier).
The pain starts when I need to make 1 + (numUsers*2) Calls in order to get a project member list, assuming I already have the project ID.
Call to projectsRoles/$projectID to get all user IDS of the current project.
Loop each USER ID+Role ID received:
Get the role name by roles/$roleID
get user information by users/$userID
That means if a project will have 30 members, the app will make 61 calls to firebase database.
My question is: Although the number of calls is high, the data received each call is minimal. I followed the instruction to make is as flat as possible, But is it common to make so much calls to firebase?

Firebase - Structuring Data For Efficient Indexing

I've read almost everywhere about structuring one's Firebase Database for efficient querying, but I am still a little confused between two alternatives that I have.
For example, let's say I want to get all of a user's "maxBenchPressSessions" from the past 7 days or so.
I'm stuck between picking between these two structures:
In the first array, I use the user's id as an attribute to index on whether true or false. In the second, I use userId as the attribute NAME whose value would be the user's id.
Is one faster than the other, or would they be indexed a relatively same manner? I kind of new to database design, so I want to make sure that I'm following correct practices.
PROGRESS
I have come up with a solution that will both flatten my database AND allow me to add a ListenerForSingleValueEvent using orderBy ONLY once, but only when I want to check if a user has a session saved for a specific day.
I can have each maxBenchPressSession object have a key in the format of userId_dateString. However, if I want to get all the user's sessions from the last 7 days, I don't know how to do it in one query.
Any ideas?
I recommend to watch the video. It is told about the structuring of the data very well.
References to the playlist on the firebase 3
Firebase 3.0: Data Modelling
Firebase 3.0: Node Client
As I understand the principle firebase to use it effectively. Should be as small as possible to query the data and it does not matter how many requests.
But you will approach such a request. We'll have to add another field to the database "negativeDate".
This field allows you to get the last seven entries. Here's a video -
https://www.youtube.com/watch?v=nMR_JPfL4qg&feature=youtu.be&t=4m36s
.limitToLast(7) - 7 entries
.orderByChild('negativeDate') - sort by date
Example of a request:
const ref = firebase.database().ref('maxBenchPressSession');
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })
Then add the user, and it puts all of its sessions.
const ref = firebase.database().ref('maxBenchPressSession/' + userId);
ref.orderByChild('negativeDate').limitToLast(7).on('value', function(snap){ })

Resources