Zenko
Zenko

Reputation: 2579

How do I ask Firestore to auto-generate the document ID but with a certain number of length?

Currently the auto-id generates 20 digits unique code. I don't have that much data and I want to make it more efficient. So I want to see if I can ask Firestore to generate a 5 or 6 digits unique id for me.

Is that possible?

Upvotes: 2

Views: 1117

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600061

The auto-generated IDs that Firestore can create are always going to be 20 characters long.

They are this long because Firestore uses their length to statistically guarantee they're unique: with this length there is no statistical chance that two clients will ever generate the same ID.

If you want to give your documents shorter IDs, you will:

  1. Have to generate your own IDs.
  2. Check against the database to see if that ID isn't already used.

This is definitely possible, but not built into any Cloud Firestore SDK, because the second step would significantly reduce performance, and also wouldn't work when the client is not connected to a server.

Upvotes: 3

Related Questions