1

I am building an application with firebase, got a query hence bringing it to this forum. The data is organised in the following pattern

Favourite
  --> Email Provider
        --> email Id
           - key (e.g. -K_EL-PS-1FJ-Dz2kqV7)
             -- attr1
             -- attr2
             -- attr3
             -- attr4 

Now Email Provider and Email ID changes for each user. There can be multiple records (favourites) for each combination. Now I would like to get all the records for Email provider and email ID combination where attr2="abc". Similarly, I would like to get the all the record for the same combination when attr3="cde". I can get his working, however getting a warning (FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn"). I can understand and appreciate the warning. Question is, how to specify the index in this scenario. I know the first level element ("Favourite"), not the second and third level element. Appreciate any help.

Kind Regards

user1687711
  • 443
  • 1
  • 4
  • 13

1 Answers1

4

When you need to add an index to a dynamic path, you indicate that dynamic path with a so-called wildcard/dollar variable:

{
  "rules": {
    "Favourite": {
      "$providerId": {
        "$emailId": {
          ".indexOn": ["attr1", "attr2", "attr3", "attr4"]
        }
      }
    }
  }
}

These $ variables are explained in the documentation on Using $ Variables to Capture Path Segments.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank. I will try this. One more question, if I can ask whether firebase support the And or Or clause e.g. attr1= "abcd" or attr2 = "def". Thanks for all help. – user1687711 Dec 30 '16 at 19:14
  • You can only have one orde and one condition in a query. Sometimes you can combine values to create a logical AND, such as I describe here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Dec 30 '16 at 21:28