I need to write rules for Cloud Firestore.
I want my rules to allow the creation of a new document in a collection:
newDoc(field1="value-1", field2="value-2", field3="other-miscellaneous-values")
only if no other document already exists in the collection with:
(field1 == "value-1") and (field2 == "value-2")
Though this is not very complicated, it seems still too complex to be found as an example in any tutorial that I found searching the net.
Beside, the user should be free to list and read all the documents in the collection if she/he wishes.
Here is what I have tried, but it does not work:
service cloud.firestore {
function alreadyExists(document) {
return exists((resource.data.field1==request.resource.data.field1)&&
(resource.data.field2==request.resource.data.field2))
}
match /databases/{database}/documents {
match /My_Collection/{anything=**} {
allow read;
allow write: if !(alreadyExists(request.resource.data));
}
}
}
I hope someone can give me some advice to get it working.