Extract from Firebase

I created Firebase DB, and I use the Push () method to save data to the database. It works great. But this problem that I am facing is when I retrieve the data. Since Push () works by creating a temporary key, I don’t know how to enter the child of this tree to sort using orderByValue (). My JSON looks like this:

{ "videos" : { "videoId1" : { "-KhxlA5-yIHYq-7PQC84" : "{\"url\":\"testUrl1\",\"title\":\"test1\",\"x\":-3.9357306957244875,\"y\":6.528204441070557,\"z\":2.5558924674987795,\"time\":7982.197021484375}", "-KhxlAEpJp30xF5n6kc5" : "{\"url\":\"testUrl2\",\"title\":\"test2\",\"x\":4.0330400466918949,\"y\":6.52820348739624,\"z\":2.3994064331054689,\"time\":2637.8599853515625}", "-KhxlAPyaGEI7AxFssG_" : "{\"url\":\"testUrl3\",\"title\":\"test3\",\"x\":-3.446043014526367,\"y\":6.52820348739624,\"z\":-3.1854910850524904,\"time\":5350.770263671875}" } } } 

My requirement is that I need to sort it based on the time key when retrieving data.

I can get to the videoId1 tree, but I don’t know how to get it. (Using reference.Child ("videos").Child (videoUrl) )

Thanks.

0
source share
1 answer

You save a string value for each child in videoId1 . There is no useful query inside this line.

If you want to query under these children, be sure to save the data as JSON, so that the general tree turns out:

 { "videos": { "videoId1": { "-KhxlA5-yIHYq-7PQC84": { "url": "testUrl1", "title": "test1", "x": -3.9357306957244875, "y": 6.528204441070557, "z": 2.5558924674987795, "time": 7982.197021484375 } ", "-KhxlAEpJp30xF5n6kc5": { "url": "testUrl2", "title": "test2", "x": 4.0330400466918949, "y": 6.52820348739624, "z": 2.3994064331054689, "time": 2637.8599853515625 } ", "-KhxlAPyaGEI7AxFssG_": { "url": "testUrl3", "title": "test3", "x": -3.446043014526367, "y": 6.52820348739624, "z": -3.1854910850524904, "time": 5350.770263671875 } } } } 

With this, you can query, as Cyril suggested:

 ref.child('videos/videoId1').orderByChild('time')... 
+1
source

All Articles