My app allows user to submit some data entry to firebase. Originally, my dataset looks like
-forms
-company1
-approvedForms
-$key
-name: 'John Doe'
-date_epoch: 1487590100
-$key2
-name: 'Taylor Smith'
-date_epoch: 1487590101
-$key3
-name: 'Sam Cesky'
-date_epoch: 1487590102
I have a page where user can type in a start and stop date to search for data within the date range. How I am doing it is the standard firebase way where I convert the search start and stop date to epoch number then call the following function
return ref.child('forms')
.child(companyId)
.child('approvedForms')
.orderByChild('date_epoch')
.startAt(startDateEpoch)
.endAt(endDateEpoch)
.once('value')
This all works fine!. However, I am starting to wonder... Say after 10 years, I may have a giant list under approvedForms. For argument sake, if I search for date 31st Jan 2020 to 1st Feb 2020, I will be forcing firebase to look through all the entry for this 2 day query
So I have thought of another idea, which is to save the entry under similar to the following where I added additional year/month. Note that the month and year is generated from the date
-forms
-company1
-approvedForms
-2017
-1
-$key
-name: 'John Doe'
-date_epoch: 1483228800
-$key2
-name: 'Taylor Smith'
-date_epoch: 1483228801
-2
-$key3
-name: 'Sam Cesky'
-date_epoch: 1485907200
In this case, I can query the data by looking into branch such as forms/company1/approvedForms/2017/1
The problem is, this is a very awkward solution for the following reasons
- If say I want to search from 2017 Feburary to 2018 Janurary, I will have to create 11 individual get request to each of the month node...
- I need to create some complex-ish algoritmy to generate the 11 paths to check if I am going over the year or not
So the question is, would I be better off sticking with the original approach because firebase is designed to do that for large sets of data in one node? Or is there a better approach to this whole problem?