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.