Firestore Document ID Contraints
- Must be valid UTF-8 characters
- Must be no longer than 1,500 bytes
- Cannot contain a forward slash (/)
- Cannot solely consist of a single period (.) or double periods (..)
- Cannot match the regular expression .*
NOTE: Refer this.
Auto Create
val doc_ref = db.collection("COLLECTION_NAME").document()val doc_id = doc_ref.id
2i5uqlufac0QvPw1YRiE
Using Base64
If the document ID is short, like an email, you can convert it to base64 url safe.
Python 3.x
import base64value = "[email protected]"# optional casefold() to convert to lowercasedoc_id = base64.urlsafe_b64encode(value.casefold().encode('utf-8')).decode('utf-8')
Output
bWVAZ21haWwuY29t
Using MD5
If the String is long or unpredictable.
Python 3.x
import hashlibvalue = "This is my id"doc_id = hashlib.md5(value.encode("utf-8")).hexdigest()
Output
eb741da8f66211d632577a593e80549e
NOTE: MD5 hex output will always be 32 characters regardless of the input value.