FireStore安全性规则:如何将更新/写入限制为文档的特定字段

时间:2020-10-28 16:40:17

标签: firebase google-cloud-firestore firebase-security

用例: 每个用户文档都有相当多的字段,但是您希望像生日之类的字段在用户注册后永远不会被更新(因此,当用户注册时,它只被写入一次,而不再被更新) 如何编写规则?

1 个答案:

答案 0 :(得分:0)

您可以使用request.resource.data。{field}

访问文档字段

在我们的示例中,它将是

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure all cities have a positive population and
    // the name is not changed
    match /users/{user} {
      allow update: if request.resource.data.dob == resource.data.dob;
    }
  }
}

这意味着只要dob不变,就可以更新文档。