What does priority mean in Firebase?
Disclosure: I work for Firebase.
Priorities are an optional (numeric or alphanumeric) value of each node, which is used to sort the children under a specific parent or in a query if no other sort condition is specified. The priority of a node is hidden from most views of the data. In cases where a priority is specified for a node, it can be found as a .priority
property in the exportVal()
of a snapshot.
Since Firebase added the ability to order children on a specified property, priorities have lost most of their value. They are a left-over artifact from the time before Firebase had orderByChild
queries. If you are starting on a Firebase project today, you should use orderByChild
instead of relying on priorities.
![nodebase](https://i.stack.imgur.com/RLxyn.jpg?s=256&g=1)
nodebase
Updated on June 20, 2022Comments
-
nodebase about 2 years
I have read the Firebase docs about priorities but I don't think I understand it yet.
I think I understand that it is related to querying and sorting data. To give my question (and the answers) some weight, in what instances might you use priorities?
From the docs I read that you can set priorities when you set a value at some reference, and then when you query that reference priority determines the ordering based on its type and value. And that makes some sense but I'm not quite understanding it.
-
martinjbaker about 8 yearsPlease update the docs with this info! It is very confusing why you'd need to use priorities at all in the current docs and from what you're saying they are unnecessary now we have orderByChild.
-
Isaac Bhasme about 8 yearsThe method
orderByChild
works only with strings. In cases where I have a number (eg: timestamp) stored as a key I should still use priorities. Am I right? -
Frank van Puffelen about 8 yearsThe parameter you pass into
orderByChild()
is the name of the property, so is naturally always a string. But you can pass any primitive JSON type intoequalTo()
,startAt()
andendAt()
. So they'll work perfectly for timestamps too. -
Bob Snyder about 8 yearsFrank: It would be helpful for the Firebase documentation team to add this information to the docs. I am an Android developer who just started using Firebase. After carefully studying the Firebase Android docs, I was puzzled about what
orderByPriority()
offered overorderByChild()
(apparently little or nothing) until I read this answer. -
Rohit Goyal about 8 years@FrankvanPuffelen Frank, is there any way to get the index/position of a particular node without loading the complete JSON. In my specific case I want the rank of a user in highscore leaderboard and the leaderboard might have millions of entries.
-
Marc M. about 8 yearsSeriously document this!
-
Al-Mothafar about 6 years@FrankvanPuffelen please we need documentation! another vote for it, why you ignore this?