Firestore create document if it doesn't exist security rule

后端 未结 1 1663
野性不改
野性不改 2021-01-27 08:32

I was trying to write a rule that if the id of the document doesn\'t exist, then create a new document. My object is:

Message message = new Message(userId, title         


        
相关标签:
1条回答
  • 2021-01-27 09:04

    You can use the exists() and get() built-in functions to verify if the document exists or to access the document so you can read its data.

    In this case, an example would be:

    match /messages/{message} {
        allow read; 
        allow create: if !exists(/databases/$(database)/documents/messages/$(request.resource.id));
    }
    

    And as @creativecreatorormaybenot suggested, you should not specify allow expressions for update and delete so the data cannot be overwritten.

    Here is a link to the documentation:

    https://firebase.google.com/docs/firestore/security/rules-conditions#access_other_documents

    https://firebase.google.com/docs/reference/rules/rules.firestore

    0 讨论(0)
提交回复
热议问题