Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Firebase data ordered by value

Tags:

firebase

Is there any way to get a certain number of Firebase data entries based on value. For example, if a certain entry had 100 children, each with a number value, would there be any way to request the children with the ten highest values?

EDIT: I realize that I could simply request the parent and iterate through the entire hierarchy of data until I find the ten with the largest value, but this would be a waste of user's data, especially if the parent contained thousands of children.

like image 407
jadengeller Avatar asked Jun 14 '26 10:06

jadengeller


1 Answers

With firebase you are dealing with a nosql database. When using a nosql database, generate your reports when you write the data, not when you read it.

eg. For the case you mentioned, maintain another object that always contains the top ten records (or pointers to them).

Every time you write a record to the database check with the top ten list, if the record belongs there too, in which case do what is necessary.

This approach is faster and more efficient than iterating through a database.

like image 71
Santosh Avatar answered Jun 16 '26 09:06

Santosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!