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
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