firestore规则来比较inpudata和现有数据

时间:2018-02-14 11:51:22

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

如何编写firestore规则来比较文档内的数据和传入的数据

实际上,只有当新数据的开始日期和门厅号不等于现有数据的开始日期和门厅号时,我才想将新文件插入或写入firestore集合。

我的数据库的结构如下图所示

enter image description here

2 个答案:

答案 0 :(得分:0)

查看firestore secuirty规则documentation

alow read, write: if request.resource.data.propertyName == resource.data.propertyName

答案 1 :(得分:0)

您需要调整文档的结构。在您的文档上设置自定义ID类似于{date} _ {hall}(即2018-05-04_1)。这样做的缺点是你需要一个firestore触发器来监视创建事件,并根据需要进行更新,以确保id的格式正确。由于Firestore安全规则的限制,我相信这是实现您所要求的唯一方法。

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

        match /events/{eventId} {
            allow read: if true;
            allow create: if !exists(get(/databases/$(database)/documents/events/$(eventId));
            allow update: if false; // Depends if you want to be able to update documents
        }

    }
}