Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce Uniqueness in a Property of a document field in Google Cloud Firestore

I have the following data as shown in the image. I want to make sure that the username field is unique. How can I enforce that? Bear in mind that my document id is already unique and I don't want to use my username as the document ID

enter image description here

like image 808
Ahmed Avatar asked Oct 31 '25 09:10

Ahmed


1 Answers

There is no way to enforce unique values of fields in Firestore. The only uniqueness you can guarantee is the document IDs within a collection, so the solution is to create a new collection where the user names are used as document IDs.

This has been discussed a few times before, so I recommend checking out:

  • Firestore security rule to check if character username already exists
  • Check a document field for a specific value in Cloud Firestore
  • I want to make unique usernames in firebase/firestore, which shows some of the relevant security rules.
  • Cloud Firestore: Enforcing Unique User Names
  • Firestore unique index or unique constraint?, a more complete write-up with code and rules samples.
  • Cloud Firestore: Enforcing Unique User Names
like image 150
Frank van Puffelen Avatar answered Nov 03 '25 01:11

Frank van Puffelen