I'm completely new to realtime database and nosql and want some help. How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
Asked
Active
Viewed 34 times
1 Answers
0
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
Firebase doesn't provide a way to filter the records by keys that start with a particular String. The most simple solution I can think of is to change the "year" property in your database to be of type number and not String, otherwise, you'll have a lexicographical order. To solve this, you need to use a query that looks like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference calendarRef = rootRef.child("calendar");
Query yearQuery = calendarRef.orderByChild("year").startAt(2000).endAt(2001);
yearQuery.addListenerForSingleValueEvent(/* ... */);
This will return all records where the "year" property holds the value of 2000.

Alex Mamo
- 130,605
- 17
- 163
- 193
-
Good to hear that. You're very welcome, cheers! – Alex Mamo Dec 14 '20 at 17:51