Firestore安全规则在调用子集合时检查父字段

时间:2020-07-20 19:51:35

标签: google-cloud-firestore

因此,我的firestore数据库的结构如下:

文档->集合-> docID->集合->文档

在docID文档中,我有一个名为 live 的字段,该字段是设置为true或false的布尔值。如果live为true,则我希望允许访问所有文档和子文档,但是如果为false,则不允许读取。我试过这样构造它:

match /collections/{docID} {
    allow read: if resource.data.live == true;
    allow write: if false;
}

此代码有什么不正确之处?

1 个答案:

答案 0 :(得分:1)

match /collection/{docID} {

  match /collections/{document=**} {
       allow read: if get(/databases/$(database)/documents/collection/$(docID)).data.live == true;
       allow write: if false;
  }

}

在这里,document = **表示所有嵌套的文档(具有相同集合以及嵌套的文档)。 还有一个简单的get调用即可读取您的{docID}文档。

您可以在这里Access other documents

找到更多信息