在 Security Firstore Rules 中是否可以只让用户根据字段值读取数据?

in Security Firstore Rules does it possible to only make users to read data depending on field value?

例如,在 Firstore 中,我有一个名为 products 的集合,每个文档都有一个名为 isAllow : false

的布尔字段

现在在 Security Firstore 规则中如何让用户只能阅读真实值为 isAllow field 的文档以及与 write 相同的文档..我阅读了文档但无法准确理解我想要的内容

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

是的,

match /collection/{document} {
  allow read: if resource.data.isAllow == true;
}

这里 resource.data 是存储在文档中的所有字段和值的映射,只有当正在访问的文档中的 isAllow 字段为真时,以上规则才允许读取操作。

正如 Dharmaraj 回答的那样,您可以只允许阅读安全规则中的那些文件:

match /products/{product} {
  allow read: if resource.data.isAllow == true;
}

但请记住 security rules are not filters 本身,而只是确保客户端不会尝试读取不允许的数据。要满足上述安全规则,您还需要使用查询来读取允许的数据:

firebase.firestore()
  .collection("products")
  .where("isAllow", "==", true)