1

After finishing up a project, we realised that we needed to have rules to make it secure. So we denormalised the realtime database

"users" :{
              ".read":"auth != null",

      "$companyId":{

        "$managedby":{

            ".indexOn": "mobile"
        }
      }
    },

So that is the new firebase rule we have. Now even after adding the index on mobile I get this warning:

util.js:233 FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "mobile" at /users to your security rules for better performance. 

I am also trying to get data from the same node using :

 return this.afDB.database.ref('users').orderByChild('mobile').equalTo(mobilenum); ( I use Angular)

The same code should work because it worked earlier, only thing now is that we are two levels deep. If I remove the orderByChild part, I download all the users. So where is the problem?

The mobilenum variable datatype is a string which matches that of the database.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user165242
  • 1,379
  • 2
  • 12
  • 18
  • I have read that the indexOn should be added before the actual node where the index happens.. It has been done that way in the above example. – user165242 Nov 23 '17 at 11:43
  • I have given the entire database a read access as true. I have hardcoded the values into the OrderByChild, but it doesnt work. But once I remove the OrderByChild it works. How many levels deep can the OrderByChild go? And secondly, why doesn't the index work.? I tried using the variables from the top node, it throws an error. – user165242 Nov 23 '17 at 12:04

1 Answers1

1

You need to define the index on the level where you execute the query, so:

"users" :{
  ".indexOn": "mobile"
},

But this may not be enough in your case. From the rules you shared it seems that the mobile property is at a dynamic path under each user/company. Firebase Database indexes a property for each child node, but the property must be at a known path.

The path cannot be dynamic, nor can there be multiple values for each node in the index.

For more on this, and ways to model your data to allow this query, see my answer here: Firebase Query Double Nested

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for the link. This cleared things for me. ".....one level deep (or with a known path) with its orderByChild and equalTo methods." I had OrderByChild go two levels in some cases, so I was confused. I now have another node for the mobile numbers. – user165242 Nov 23 '17 at 16:43
  • Good to hear that answer clarified it. Expanding your data model is an unfortunate fact of working with NoSQL databases. A fact that also immediately explains the scalability benefits of NoSQL databases. :-) – Frank van Puffelen Nov 23 '17 at 16:56