pythonfirebase-realtime-databasepyrebase

How to skip data points in Firebase query?


I'm not sure if this is possible, but I have a data table in Firebase that looks like this:

Key1:
   Timestamp: 1
   Value: 10
Key2:
   Timestamp: 2
   Value: 15
Key3:
   Timestamp: 3
   Value: 13
Key4:
   Timestamp: 4
   Value: 14
...

I can easily filter data by timestamp by running something like (in Pyrebase)

pyrebase.database().child("My_Dataset").order_by("Timestamp").start_at(1).end_at(4)

Which would return only Keys 1 --> 4

Is it possible though to run a query using my timestamps that would only return, say, keys 1 and 3? i.e.

pyrebase.database().child("My_Dataset").order_by("Timestamp").start_at(1).end_at(4).step(2)

I have managed to figure out a pseudo-mechanism of doing this by running a method like

def stepped_query(start,end,step):
  data = []
  for i in range(start,end,step):
     data.append(pb.database().child("My_Dataset").order_by("Timestamp").start_at(i).limitToFirst(1))

But due to the volume of queries this requires, it is far from ideal.

Is there thus any way to implement a query in Firebase that can jump over certain datapoints using a predefined step size?


Solution

  • Firebase Realtime Database queries return a single, contiguous slice of the child nodes. There is no way to skip some of the results in the slice that is returned.

    If you only want some of those nodes, you can either post-process the result in your application code after retrieving them, or pre-process the data to allow the query you want.

    I'm not exactly sure how that would work here, but say you'd want only odd results, you could store Math.floor(Timestamp / 2) as a property for each child.