How to order the nodes in firebase console based on key

后端 未结 1 808
南笙
南笙 2020-11-28 17:03

I have a node for users with key 1 to 1400 but it\'s not in order. Hot to view this in order in the console. I had a similar experience with another node. But it later got o

相关标签:
1条回答
  • 2020-11-28 17:40

    Unfortunately, you cannot change the order of the nodes in the Firebase Database Console. By default, all the nodes are ordered by key. One thing to remember is that Firebase keys are Strings. And when strings are order, are ordered lexicographically.

    So for numbers, this is the normal order:

    • 1308
    • 1309
    • 1310
    • 1311

    But for strings, this is the normal order:

    • "1308"
    • "1309"
    • "131"
    • "1310"

    There is no operator in Firebase and as far as i know nor in most other databases that allow you to change this behavior. Instead, you will have to modify the data to get the behavior you want. So, store values that are in the order you need them when sorted lexicographically. For numbers you can accomplish that by padding them with zeroes:

    • "0131" //zero added before
    • "0132" //zero added before
    • ......
    • "1308"
    • "1309"
    • "1310"
    • "1311"
    0 讨论(0)
提交回复
热议问题