我可以在firebase中创建一个规则来查询push()数组中的值吗?

时间:2016-06-04 18:07:53

标签: firebase-realtime-database

我只希望用户能够加载组,如果他们的列表中有组,我将如何为其编写规则?

或者有没有办法编写规则来查找firebase推送数组中的值?

Database Schema

例如,我想写一条规则可能看起来像这样。这条规则无效,但旨在用它来解释我的观点。

"groups":{
  "$group_id":{
    ".read":"root.child('users').child(auth.uid).child('groups').hasChildValue().val() == $group_id"
  }
},

我只希望用户能够加载组,如果他们的列表中有组,我将如何为其编写规则?

更新,我是如何修复它的。 - 将数据重组为持平。摆脱使用push()来添加值。 - 平面数据使得引用密钥变得容易。

Fixed Structure

"groups":{
        // root/users/auth.uid/groups/$group_id
        "$group_id":{
          // only read if the user has the group_id
          ".read":"root.child('users').child(auth.uid).child('groups').child($group_id).exists()",
          // only write if logged in and it's new || if the user has group id
          ".write":"(auth != null && !data.exists() && newData.exists()) || root.child('users').child(auth.uid).child('groups').child($group_id).exists()"
      }
    },

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试“过滤”群组数据,这不是Firebase规则的用途。参见链接以供参考:
https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters

对于您试图实现的目标(限制对组的读取权限),您需要将数据模型调整为应用程序访问它的方式。如果这是您正在寻找的,请告诉我,我可以更新我的答案。