0

I need a firebase rule, where the document is only created if the value of certain property does not exists in any other document of this collection (myCollection).

pseudo-code:

function isValid() {
  let newProp = request.resource.data.prop1;
  let collection = /databases/{database}/documents/myCollection;
  let same = collection.where("prop1", "==", newProp);
  return !exists(same);
}

If I don't miss understood this post, it is not possible even if there exists a default document:

In fact, you can not ensure uniqueness of any given document field for a create, since it's not possible in security rules to query all documents in the collection for existence for that field.

Is it still like that?

Hölderlin
  • 424
  • 1
  • 3
  • 16

1 Answers1

1

The only way to do this is to name your document in myCollection with the value of the key.

Pseudo-code:

function isValid() {
  let newProp = request.resource.data.prop1;
  let doc = /databases/{database}/documents/myCollection/{newProp};
  return !exists(doc);
}
l1b3rty
  • 3,333
  • 14
  • 32