我想写一条规则,不允许第二次添加相同的文档 [英] I want to write a rule that will don't allow add same document second time

查看:46
本文介绍了我想写一条规则,不允许第二次添加相同的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{usersID} {
    allow read;
    allow write: if request.auth.uid != null;
        match /desksCollection/{desksCollectionID} {
        allow read;
        allow write: if resource.data.DeskName != request.resource.data.DeskName;
      }
    }
  }
}

我想写一条规则,不允许第二次添加相同的文档. 这里有什么不对吗?

I want to write a rule that will don't allow add same document second time. What is incorrect here ?

推荐答案

安全规则无法实现您要尝试执行的操作.这将要求您能够在desksCollection上查询符合具有一定值的字段的条件的文档.但是,在安全规则中无法执行查询.您可以按其ID提取文档,但这对您没有帮助,因为您不知道要提取哪个文档.

What you are trying to do is not possible with security rules. This would require that you be able to perform a query on desksCollection for documents that match the criteria of having a field with a certain value. However, performing queries is not possible in security rules. You can fetch a document by its ID, but that won't help you here, because you don't know which document to fetch.

如果您要求deskName是唯一的,请考虑使用该值作为文档的ID.然后,您可以编写一条规则来阻止更新(如果该文档已经存在).

If you require that deskName be unique, then consider using that value as the ID of the document. Then you can write a rule to prevent updates if the document already exists.

这篇关于我想写一条规则,不允许第二次添加相同的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆