【问题标题】:Firebase indexOn rules in unique keys唯一键中的 Firebase indexOn 规则
【发布时间】:2017-07-11 10:55:43
【问题描述】:

我在通过 firebase 数据库规则索引数据时遇到问题。

我的数据如下:

entities
 $entity_id
  users
   $user_id : true

我的 firebase 3.x 查询如下:

firebase.database().ref('entities')
  .orderByChild('users/' + user.uid)
  .equalTo(true)
  .once('value')

我的规则如下:

{ 
  "entities": {
    "users": {
      "$userId": {
        ".indexOn": ".value"
      }
    }
  }
}

但我还是得到了这个:

FIREBASE WARNING:
Using an unspecified index.
Consider adding ".indexOn": "users/VShAzNsxGjOIIlcrrZsWCxuMuuH2"
at /entities to your security rules for better performance

我试过了:

Firebase Security Rules: .indexOn unique ids

Firebase .indexOn dynamic keys

感谢您的帮助

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-security


    【解决方案1】:

    删除任何 .indexOn 属性。

    键会自动编入索引,因此无需显式这样做。

    https://firebase.google.com/docs/database/security/indexing-data

    【讨论】:

      【解决方案2】:

      解决方案是更好的 noSQL 架构。

      该查询将尝试过滤所有实体并检查是否找到了 userId。

      实际上,您必须有一个类似 userEntities 的 ref,其中将包含该用户所属的所有实体键 - 并通过这些键获取该用户的实体。

      您必须注意的一件事是,为 firebase 设计数据库是 sequrity 规则。顺序规则最终决定了这种类型的查询在客户端是否可以接受。 示例:

      firebase.database().ref('entities').orderByChild('users/' + user.uid)

      是对“实体”根的调用,而不是单独对“实体/用户/用户 ID”的调用。因此,如果您想禁用除您自己之外的所有其他实体的读取,您必须尝试访问

      .ref('entities').child(entityId)

      这在任何地方都不够突出。

      关系数据库赢得了这一 IMO。

      【讨论】:

        【解决方案3】:

        .indexOn 应与动态键平行放置,如下所示:

        { 
          "entities": {
            "users": {
              ".indexOn": ".value",
            }
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-09-29
          • 2016-06-17
          • 2017-09-01
          • 2019-09-21
          • 2016-06-03
          • 2017-01-07
          • 2018-12-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多