Firebase: Is it reliable to use a shortened version of a document ID?

Viewed 30

I want to add a new field in my candidates collection which is called shortCandidateId. This ID would be a substring of the first 5 characters from the actual ID of the document, and in lower case.

For example:

Regular Id from Firebase version: 0KdeD344kfed
Shortened version: 0kded

Is it reliable to use this shortened version to get the data from the document?

Are there possible collision when transforming the candidate ID to this shortened version?

97ADsjuTPxOktx5FC1gx = 97aDs
97ADsjuTPxOktx5FC12x = 97aDs

In this case I'd have two candidates with the same ID and I could not fetch the correct one.

1 Answers

Any time you put the same amount of information in smaller set of bits, you'll have a chance of collisions. If there wasn't, the original naming scheme would be wasteful.

So the question is not whether there can be collisions, but how common those will be and how you deal with them. If you don't want to deal with the collisions, I'd recommend to not change the identifier scheme and use the document IDs as Firestore generates them for you.

Related