Here is my current data design on Firebase (data is simply a list of jsons at the "events" reference). The fields timestamp, action, and duration are fixed, and there might be more optional fields added later.
backend-7f34e
events
-KQ30Lc6lasdfasdfAi1URf
action: "Get ready"
duration: 100
timestamp: 1472167504389
-KQ30MM8Fgasdf_o10TW
action: "Run"
duration: 1890
timestamp: 1472167507411
I need to be able to retrieve data from a timestamp range [start, end]. Do I need to use timestamp as the key? What would be a better way to structure the data?
Use OrderByChild combined with startAt and endAt
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference eventsReference = databaseReference.child("events");
Query query = eventsReference.orderByChild("timestamp").startAt(1472167504389);
query.addValueEventListener(new ValueEventListener).....
....
StartAt takes double as the argument, so structure your data accordingly.
More info - https://www.firebase.com/docs/web/api/query/startat.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With