如何在阵列包含特定值的地方设置Firestore安全规则

问题描述

我想这样设置规则:当用户的uid包含在城市文档的数组字段中时,该用户具有权限。

我做到了。请告诉我怎么做

allow read : if (city.some-array.contain(uid));

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'cities' collection
    match /cities/{city} {
      allow read: ***if <condition>;***
      allow write: if <condition>;
    }
  }
}

解决方法

您可以将resource.data(该对象包含操作前的当前数据)与in结合使用以实现此目的。

match /cities/{city} {
    allow read,write: if request.auth.uid in resource.data.authorizedUsers
    // replace authorizedUsers with the name of your array field
}