I have a program performing a batch write on firestore to a collection.
I am trying to write the document only if is doesn't exists in the collection and skip without modifying anything if it does.
the security rule is as below:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /collection/{id} {
allow create,delete: if request.auth.uid != null;
}
}
}
While testing on the test tool, it working as expected. I am only allowed to create and delete documents in this collection. However when running from the program, the batch write is able to modify the document field (stamp in this case) if is already exists.
The code is as follows:
var batch = firestore.batch();
var docRef= firestore.collection("collection").doc(data.id);
batch.set(
docRef,{
id: data.id,
stamp: new Date(),
},
);
What am I missing, or doing wrong?