-1

database structure

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?

Mohitds96
  • 1
  • 3

1 Answers1

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