14

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?

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Moemanchu
  • 161
  • 1
  • 1
  • 5
  • As far as I know, this deeper-level (or multi-value) indexing is not (yet) possible. We touched on something similar here (http://stackoverflow.com/questions/26910242/querying-nested-data-in-firebase/26911429#26911429) and I haven't seen any response from a Firebaser saying that it **is** possible; not have I been able to get non-trivial (i.e. multi-value.) keys working. So for the moment it looks like you will have to create your own index for these, as documented in the answer I linked to. – Frank van Puffelen Nov 28 '14 at 00:55

2 Answers2

30

You can query and index arbitrarily deep now.

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

and query on nested values:

.orderByChild('stats/height')
Tom Larkworthy
  • 2,104
  • 1
  • 20
  • 29
18

Update 2015/12/17: Firebase now supports deep indexing, so see Tom's answer.


You can arbitrarily nest indexing rules in your data tree, though you can only query the attributes of one list of elements at a time - not arbitrary deep. Here's some example rules, to index height:

{
  "rules": {
    "$dinosaur": {
      "stats": {
        ".indexOn": ["height"]
      }
    }   
  }
}
Rob DiMarco
  • 13,226
  • 1
  • 43
  • 55
  • So how would I query this? I tried setting up the query with something like the following but nothing get's returned. Thanks for your help. `var fb = new Firebase("https://a_fb_ur./dinosaur");` `fbQuery = fb.orderByChild("height").equalTo(4);` – Moemanchu Feb 10 '15 at 02:44
  • You'll have to flatten your data in order to do that query, and then you need to attach a listener to retrieve data. i.e. `new Firebase(...).child('dinosaurs').orderByChild('height').on('child_added', function(snapshot) { ... })`. – Rob DiMarco Feb 10 '15 at 18:59
  • Okay; For storing [GeoJSON](http://geojson.org/geojson-spec.html#examples) I'll probably want to flatten the data then cause I'll commonly want to index and search on keys stored in the nested "properties" key. Thanks for confirming. – Moemanchu Feb 11 '15 at 05:04
  • complete query is `new Firebase(...).child('dinosaurs').orderByChild('height').equalTo(4).on(...)` – Nicolas Janel Nov 06 '15 at 14:14
  • anyway the Firebase doc doesn't give exemple of wildcard node applied on indexes, as it applied in security rules, it's logical it works for indexes too. Thanks to get the idea – Nicolas Janel Nov 06 '15 at 14:16
  • This answer is out of data, Firebase now supports deep indexing (see my answer) – Tom Larkworthy Dec 17 '15 at 18:48