0

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

  1. 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...
  2. 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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ErnieKev
  • 2,831
  • 5
  • 21
  • 35
  • For #1: Firebase get calls are not as slow as you may think. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Feb 20 '17 at 15:07
  • It's only my opinion, but you migth be using the wrong tool for the job, firebase is a dream come true when it comes to deliver the "real" time feature, it radically reduce the ammount of you you have to write, plus all the other options packaged like messaging, analytics and so on, but is not a relational database, which could be a way better option for your case. – Camilo Casadiego Sep 22 '17 at 18:07

0 Answers0