Don't store documents with duplicated key in Firestore

I want to avoid duplicated keys in different documents.

For example, in the document 1 I have this:

{
  "user": "myuser",
  "email: "a@a.com"
}

In the document 2 I have:

{
  "user": "myuser2",
  "email: "b@b.com"
}

And in document 3 I have:

{
  "user": "myuser",
  "email: "c@c.com"
}

I don't want to be possible to create document 3, as the user key has the same value. Is possible to do this with database rules?

Thanks.

1 answer

  • answered 2018-01-14 15:14 Badr

    add the document with user as a unique ID like that

    db.collection("Your Collection").document(user).set(Your Object)
    

    "user" is the user field of your object