What does it really serve .indexOn in firebase


For some time now, I have been using firebase , however, the purpose of the rule .indexOn is not clear to me. This streamlines searches on secondary data or something like that?.

for example:

If I have a database with the following structure:


and in the rules I define:


this will speed up the query of the fields defined in .indexOn

asked by Revsky01 24.08.2018 в 23:48

1 answer


Firebase allows you to query data using a arbitrary secondary key. If you know in advance what your  indexes, you can define them through the rule .indexOn

Review the documentation example:

The simplest way to explain this is through an example. Here in Firebase we are lovers of dinosaurs. Next, we will include a fragment a sample database with information about dinosaurs. We will use it to explain how .indexOn works with orderByChild() .

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

Imagine that, in our app, we often have to order dinosaurs by name, height and length, but never by weight. To improve the performance of our queries, we can give that information to Firebase .

Because the names of dinosaurs are only the keys, Firebase optimizes in advance queries by name of the dinosaur, since this is the key to the record. We can use .indexOn to tell Firebase that it also optimizes queries for height and length.

  "rules": {
    "dinosaurs": {
      ".indexOn": ["height", "length"]

Like other rules, you can specify a .indexOn rule at any level of your rules. In the previous example, we place it at the root level, since all the dinosaur data is stored in the root of the database.

answered by 24.08.2018 / 23:54