rules_version = \'2\';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{usersID} {
allow read;
allow write: if request.auth.
Here's how I avoid duplication for tokens You can do it for any field.
First query through document and if null
then add that collection.
@override
void initState() {
// TODO: implement initState
super.initState();
firebaseMessaging.getToken().then((token){
print(token);
bool isAvailabe;
Firestore.instance.collection("tokens").where("token",isEqualTo: token).getDocuments().then((snap){
if(snap == null){
Firestore.instance.collection('tokens').add({"token":token}).catchError((error){
print(error);
}).catchError((e){
print(e);
});
}
});
});
}
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.
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.