Bird
Raised Fist0
Firebasecloud~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a document ID in Firebase?
easy
A. To uniquely identify each record in a collection
B. To store user passwords securely
C. To define the database location
D. To control access permissions

Solution

  1. Step 1: Understand what a document ID represents

    A document ID is a unique identifier for each record in a Firebase collection, ensuring each document can be accessed individually.
  2. Step 2: Differentiate from other database concepts

    Document IDs are not used for storing passwords, defining database location, or controlling access permissions.
  3. Final Answer:

    To uniquely identify each record in a collection -> Option A
  4. Quick Check:

    Document ID = Unique record identifier [OK]
Hint: Document ID uniquely names each record [OK]
Common Mistakes:
  • Confusing document ID with security rules
  • Thinking document ID stores data
  • Mixing document ID with database path
2. Which of the following is the correct way to let Firebase generate a document ID automatically?
easy
A. Use collectionRef.doc() without arguments
B. Use collectionRef.doc('customID')
C. Use collectionRef.add(data)
D. Use collectionRef.set(data)

Solution

  1. Step 1: Identify Firebase methods for adding documents

    The add() method adds a document with an auto-generated ID to a collection.
  2. Step 2: Understand difference from doc() and set()

    doc() with no argument creates a reference but does not add data; set() requires a document reference.
  3. Final Answer:

    Use collectionRef.add(data) -> Option C
  4. Quick Check:

    Auto ID creation = add() method [OK]
Hint: Use add() to auto-generate document ID [OK]
Common Mistakes:
  • Using doc() without set() does not create document
  • Assuming set() auto-generates ID
  • Passing custom ID when auto ID is needed
3. Given this code snippet, what will be the document ID of the new document?
const docRef = collectionRef.doc('user123');
docRef.set({name: 'Alice'});
medium
A. 'user123'
B. Auto-generated random ID
C. 'Alice'
D. Undefined

Solution

  1. Step 1: Analyze the doc() method argument

    The doc('user123') call creates a document reference with ID 'user123'.
  2. Step 2: Understand set() usage

    The set() method writes data to the document with the specified ID.
  3. Final Answer:

    'user123' -> Option A
  4. Quick Check:

    doc('user123') sets document ID = 'user123' [OK]
Hint: doc('ID') sets document ID explicitly [OK]
Common Mistakes:
  • Thinking set() changes the ID
  • Assuming ID is auto-generated here
  • Confusing document data with ID
4. What is wrong with this code if the goal is to create a document with an auto-generated ID?
const docRef = collectionRef.doc();
medium
A. doc() requires a string argument
B. doc() without arguments does not create a document automatically
C. set() cannot be used with doc()
D. The data object is missing an ID field

Solution

  1. Step 1: Understand doc() without arguments

    Calling doc() without arguments creates a reference with a random ID but does not create the document automatically.
  2. Step 2: Confirm the issue

    The code only creates a DocumentReference; to create the document, set(data) or add(data) is needed.
  3. Final Answer:

    doc() without arguments does not create a document automatically -> Option B
  4. Quick Check:

    doc() alone ≠ document creation [OK]
Hint: doc() alone doesn't create document, set() must follow [OK]
Common Mistakes:
  • Assuming doc() creates document immediately
  • Thinking set() is invalid here
  • Expecting doc() to require argument always
5. You want to create user profiles with document IDs matching their usernames for easy lookup. Which strategy is best?
hard
A. Create documents without IDs and rely on collection order
B. Let Firebase auto-generate IDs and store username inside document
C. Use random UUIDs as document IDs and index usernames separately
D. Use usernames as document IDs by calling doc(username)

Solution

  1. Step 1: Understand the goal

    The goal is to have document IDs that match usernames for easy direct lookup.
  2. Step 2: Evaluate strategies

    Using doc(username) sets the document ID to the username, enabling direct access by username.
  3. Step 3: Compare with other options

    Auto-generated IDs or random UUIDs require extra queries to find username; no IDs or relying on order is unreliable.
  4. Final Answer:

    Use usernames as document IDs by calling doc(username) -> Option D
  5. Quick Check:

    Username as ID = direct lookup [OK]
Hint: Use username as doc ID for direct access [OK]
Common Mistakes:
  • Relying on auto IDs for username lookup
  • Ignoring need for unique IDs
  • Assuming collection order is stable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard