firestore - unique field

1,590 views
Skip to first unread message

Martin Kuhn

unread,
Oct 17, 2017, 4:27:57 PM10/17/17
to Firebase Google Group
Is it possible to define a "unique" field (via an index or so)?
 
This field should be additional to the doc id. 

M

unread,
Dec 4, 2017, 2:48:57 PM12/4/17
to Firebase Google Group
At the moment it would seem the answer is no. An index will automatically be created on any unique fields, however, you yourself will have to maintain its uniqueness.

You can't even use transactions to keep a field unique because the transactions get() method does only support single document references, not queries. I guess it makes sense since keeping track of an arbitrarily large amount of documents is somewhat difficult. Perhaps it could simply be monitoring for any writes to the collection in question? So the transaction only succeeds if no other writes have been made to the collection in the meantime? But this could also backfire if the collection is really big with frequent writes. Well, what do I know... nothing. :)

Sadly though, it basically means you have to make a database write, cross your fingers and hope that consistency is kept.

Feature request: we need some way of keeping fields unique! :)

Temporary solution(ish): Move the field into its own collection and maintain uniqueness through the document IDs, then reference the ID of the document the field was supposed to be in. However, this will probably double your reads and writes... so not a cheap solution.
Reply all
Reply to author
Forward
0 new messages