Firebase Security Rules: .indexOn unique ids

心不动则不痛 提交于 2019-12-21 01:13:33

问题


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, ... )

回答1:


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.



来源:https://stackoverflow.com/questions/37551951/firebase-security-rules-indexon-unique-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!