Search code examples
firebaseionic-frameworkfirebase-realtime-databaseionic3angularfire2

Firebase query download the whole database. Why?


I try to download and show only specific data from the Realtime Database. I have the following code:

getUserPlatformIos() {
    this.dataRef = this.afDatabase.list('data/users', ref => ref.orderByChild('meta/platform').equalTo('ios'));
      this.data = this.dataRef.snapshotChanges().map(changes => {
        return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
      });
      return this.data;
  }

My firebase database structure enter image description here

Firebase rules enter image description here

Why firebase does download the whole database if I query before? This causes very long loading times and a lot of downloaded data....


Solution

  • Indexes need to be defined at the place where you the query. Since you run the query on data/users, that's where you need to define your index:

    "users": {
      ".indexOn": "meta/platform"
    }
    

    This defines an index on users, which has the value of the meta/platform property of each user.

    Note that the log output of your app should be showing an error message with precisely this information. I highly recommend checking log output whenever something doesn't work the way you expect it to work.