📖 Scenario: You are building a simple app that stores user profiles in a Firebase Firestore database. Each user profile needs a unique document ID. You want to learn different ways to assign these document IDs when adding data to Firestore.
🎯 Goal: Learn how to create Firestore documents with different document ID strategies: auto-generated IDs, custom string IDs, and using user emails as IDs.
📋 What You'll Learn
Create a Firestore collection called users.
Add a user document with an auto-generated ID.
Add a user document with a custom string ID.
Add a user document using the user's email as the document ID.
💡 Why This Matters
🌍 Real World
In real apps, choosing the right document ID strategy helps organize data and makes it easier to find and update records.
💼 Career
Understanding Firestore document IDs is essential for backend and full-stack developers working with Firebase to build scalable cloud applications.
Progress0 / 4 steps
1
Create Firestore collection and add a user with auto-generated ID
Write code to add a user document to the users collection with fields name set to "Alice" and age set to 30. Use Firestore's auto-generated document ID feature by calling add() on the collection reference.
Firebase
Hint
Use addDoc() with the collection reference and an object for the user data.
2
Add a user document with a custom string ID
Write code to add a user document to the users collection with document ID "user_bob". The user data should have name set to "Bob" and age set to 25. Use the setDoc() function with a document reference created by doc().
Firebase
Hint
Create a document reference with doc() using the collection name and custom ID, then use setDoc() to add data.
3
Add a user document using the user's email as the document ID
Write code to add a user document to the users collection with document ID set to the user's email "carol@example.com". The user data should have name set to "Carol" and age set to 28. Use doc() and setDoc() functions.
Firebase
Hint
Use the email string as the document ID by passing it to doc(), then add data with setDoc().
4
Complete Firestore document ID strategies example
Ensure the code includes all three user additions: one with auto-generated ID for Alice, one with custom ID "user_bob" for Bob, and one with email ID "carol@example.com" for Carol. The code should import all necessary Firestore functions and initialize the Firestore database.
Firebase
Hint
Make sure all previous steps are combined and all imports and variables are included.
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
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.
Step 2: Differentiate from other database concepts
Document IDs are not used for storing passwords, defining database location, or controlling access permissions.
Final Answer:
To uniquely identify each record in a collection -> Option A
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
Step 1: Identify Firebase methods for adding documents
The add() method adds a document with an auto-generated ID to a collection.
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.
Final Answer:
Use collectionRef.add(data) -> Option C
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?