0

I have a Firestore document collection called registrations. Inside registrations, there are documents that contain name, phone number, and age of persons. I want to write the security rules database which allows the creation of person document if and only if another person with the same name does not exist on the database

Thanks in advance

Sachin Titus
  • 1,960
  • 3
  • 23
  • 41

1 Answers1

2

It sounds like you want to keep your names distinct, such as with a UNIQUE constraint in SQL.
It is not possible to query specific document fields in Firestore security rules.
There are two ways to do what you want with Firestore:

  • use name as your document id
  • or create a collection /usedNames with name as document id, so that in your security rule you can test:
let name = request.resource.data.name;
return exists(/usedNames/$(name)) == false
Louis Coulet
  • 3,663
  • 1
  • 21
  • 39