Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .indexOn with nested keys in Firebase

Tags:

json

firebase

I have a a bunch of keys that i want to index using .indexOn

Suppose my data looks like the following. I want to be able to use .orderByChild("height").

{
  "lambeosaurus": {
    "stats": {
      "height" : 2.1,
      "length" : 12.5,
      "weight": 5000
    }
  },
  "stegosaurus": {
    "stats": {
      "height" : 4,
      "length" : 9,
      "weight" : 2500
    }
  }
}

How wound I specify the rule for indexing height which is a child of stats? Do I have to restructure or flatten my data?

like image 688
Moemanchu Avatar asked Nov 27 '14 21:11

Moemanchu


People also ask

How many indexes types are supported in the Firebase realtime database?

Cloud Firestore uses two types of indexes: single-field and composite.

What is getKey () in Firebase?

Once the push method has created a new child, it returns a DatabaseReference object initialized with the path to that child. Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value.

Is there primary key in Firebase?

This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.


1 Answers

You can query and index arbitrarily deep now.

{
  "rules": {
    "$dinosaur": {
      ".indexOn": ["stats/height"]
    }   
  }
}

and query on nested values:

.orderByChild('stats/height')
like image 50
Tom Larkworthy Avatar answered Sep 20 '22 19:09

Tom Larkworthy