Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Security Rules: .indexOn unique ids

I have this structure:

"post": {
   "groupMember": {
     "-KHFHEHFWDB1213": "true",
     "-KSJHDDJISIS011": "true",
     "-KJSIO19229129k": "true"
   }
}

I want to .indexOn the auto-generated unique groupMember ids.

I tried doing this:

"post": {
  ".indexOn": "groupMember"
  ...
}

But I'm still getting this error:

Consider adding ".indexOn": "groupMember/-KHFHEHFWDB1213" at /post

So how do I add a .indexOn on a unique id?

UPDATE:

This is the query I use (in Swift):

postRef.queryOrderedByChild("groupMember/\(uid)").queryEqualToValue(true).observeEventType(.Value, ... )
like image 889
leonardloo Avatar asked May 31 '16 17:05

leonardloo


People also ask

Can Firebase handle 10 million users?

The limit you're referring to is the limit for the number of concurrently connected users to Firebase Realtime Database on the free Spark plan. Once you upgrade to a payment plan, your project will allow 200,000 simultaneously connected users.

How do I set rules in Firebase database?

These rules are hosted on Firebase servers and are applied automatically at all times and you can change the rules of your database in Firebase console. You just have to select your project, click on the Database section on the left and select the Rules tab.

How do I find my unique key in Firebase?

You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.

How many rules are you used to secure Realtime Database?

The RTDB has only three rule types: . read. .


1 Answers

To query whether or not one of the given keys has value "true" (note that as typed your data structure is a string, not a boolean -- something to look out for!) you'll want to create security rules like so:

{
  "post": {
    "groupMember": {
      "$memberId": {".indexOn": ".value"}
    }
  }
}

And then use queryOrderedByValue("true") on the /post/groupMember path. Important to note is $memberId in the rules, which declares a wildcard key representing your autogenerated ids, and .value, which indicates that rather than indexing on a child property (if your structure had another layer of nesting) you want to index on the immediate leaf node value.

like image 169
Michael Bleigh Avatar answered Sep 29 '22 02:09

Michael Bleigh