Jakub Szlaur
Jakub Szlaur

Reputation: 2132

Firebase: startAt() is not filtering data stored with keys as hours since epoch

My data is stored with keys as hours since epoch. I want to get only slice of the data - so for example I want to specify that I want all data starting at 453615.

So far I got this code:

  this.dbService.databaseRef.child('dev_trends/' + this.deviceUid)
  .orderByKey()
  .startAt('453615')
  .get().then(dataTrends => {
    const data = dataTrends.val();

    console.log(data);
  });

With the code above I get this results in my console:

enter image description here

My data model looks like this:

enter image description here

What should I change in my code so that I get the data from Firebase filtered?


I create the databaseRef variable inside a service with this code:

  constructor(private db: AngularFireDatabase) {
    //Set what happens when we read all devices
    this.databaseRef = db.database.ref(); //Create databaseReference
  }

Json of my data:

  {"dev_trends" : {
    "-MkMxxZXXzmKclgeGPde" : {
      "H" : {
        "453614" : "50;45",
        "453615" : "50;50",
        "453617" : "50;55",
        "453630" : "51;60"
      },
      "T" : {
        "453614" : "24;18",
        "453615" : "23;17",
        "453617" : "23;15",
        "453630" : "22;10"
      }
    }
  }}

Upvotes: 3

Views: 124

Answers (1)

Jakub Szlaur
Jakub Szlaur

Reputation: 2132

Find out where the problem was:

  • The startAt() method was filtering data with keys H and T and not the lists of data inside those keys.
  • Solution was to restructure the data model.

New data model:

enter image description here


Additional info:

  • I am not saying my solution is ideal - but for my specific problem it will do the trick.
  • It will still require some additional work for me - so if anyone else has a better solution for this please post Your answer too :)

Upvotes: 1

Related Questions