0
0
Firebasecloud~10 mins

Document ID strategies in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Document ID strategies
Start Document Creation
Choose ID Strategy
Auto-ID
Generate
Save Document with ID
End
This flow shows how a document ID is chosen when creating a document: either auto-generated, custom provided, or composed from fields, then saved.
Execution Sample
Firebase
db.collection('users').doc().set({name: 'Ana'})
db.collection('users').doc('user123').set({name: 'Bob'})
db.collection('users').doc(user.email + '_profile').set({name: 'Cara'})
Creates three documents in 'users' collection using auto-generated ID, custom ID, and composite ID.
Process Table
StepActionDocument IDResult
1Create doc with auto-IDauto-generated (e.g. 'XyZ123')Document saved with auto-generated ID
2Create doc with custom ID 'user123'user123Document saved with ID 'user123'
3Create doc with composite ID 'user@example.com_profile'user@example.com_profileDocument saved with composite ID
4End-All documents created successfully
💡 All documents created with chosen ID strategies
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
docIDnoneauto-generated stringuser123user@example.com_profilefinal IDs assigned
Key Moments - 3 Insights
Why does the first document have a strange ID like 'XyZ123'?
Because in step 1, the ID was auto-generated by Firebase, as shown in execution_table row 1.
Can I use any string as a custom document ID?
Yes, but it must be unique in the collection and cannot contain slashes. This is shown in step 2 where 'user123' is used.
What is a composite ID and why use it?
A composite ID combines fields like email and suffix to create a meaningful unique ID, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of ID is assigned at step 1?
AComposite ID
BCustom ID
CAuto-generated ID
DNo ID assigned
💡 Hint
Check the 'Document ID' column in execution_table row 1
At which step is the document ID explicitly set by the user?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look for 'custom ID' in the 'Action' column in execution_table
If you want to create a document ID combining user email and '_profile', which step shows this?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
See 'composite ID' in execution_table row 3
Concept Snapshot
Document ID strategies in Firebase:
- Auto-ID: Firebase generates a unique ID automatically.
- Custom ID: You provide a unique string as the document ID.
- Composite ID: Combine fields (like email + suffix) to form an ID.
Choose based on uniqueness and readability needs.
IDs must be unique within a collection.
Full Transcript
When creating documents in Firebase, you must decide how to assign the document ID. You can let Firebase generate a unique ID automatically, which looks like a random string. Alternatively, you can provide your own custom ID string, which must be unique and cannot contain slashes. Another option is to create a composite ID by combining meaningful fields, such as a user's email plus a suffix. This helps keep IDs readable and unique. The execution table shows three steps creating documents with these strategies, tracking the assigned IDs and results. Understanding these options helps you organize your database effectively.