Search code examples
pythonfirebasefirebase-realtime-database

How to index and retrieve ordered firebase nested data (3 level) using Python


I have firebase json data with the below structure:

enter image description here

Below is the index rule I have in Firebase:

"rules": {
    "Trip": {
            ".indexOn": ["tripLastUpdateTimestamp"]
       }
   }

And this is the Python code I used for retrieving the firebase data based on the trip_LastUpdateTimestamp:

ref = db.reference('Trip')
snapshot = ref.orderbychild('tripLastUpdateTimestamp').start_at(1527647100).end_at(1527647200).get()

But the above code doesn't return any data.

Can you guys please help me to fix this problem? would be really helpful if you guys let me know what I'm doing wrong.


Solution

  • Your reference should be

    ref = db.reference('Trip/Trial_345')
    

    or, if you need to use a variable child id:

    subRef = xyz //<-- set your variable value here
    ref = db.reference('Trip/' + subRef)
    

    Have a look at the documentation here

    You rules shall also be adapted, with the same logic, i.e. at the level of the node that is the direct parent of the children nodes you want to order through orderbychild()