Adding Documents in Firebase: Using add() vs set()
📖 Scenario: You are building a simple app to store user profiles in Firebase Firestore. You want to learn how to add new user documents to a collection using two different methods: add() and set().This will help you understand how to create documents with automatic IDs and how to create or overwrite documents with specific IDs.
🎯 Goal: Build a Firebase Firestore setup where you first create a collection called users and add user documents using add() and set() methods correctly.
📋 What You'll Learn
Create a Firestore collection reference called usersRef pointing to the users collection
Add a new user document with automatic ID using add() with exact data
Create a user document with a specific ID using set() with exact data
Use correct Firestore syntax for both methods
💡 Why This Matters
🌍 Real World
Adding and managing user data in Firestore is common in apps like social networks, e-commerce, and content management systems.
💼 Career
Understanding Firestore document creation methods is essential for cloud developers working with Firebase backend services.
Progress0 / 4 steps
1
Create Firestore collection reference
Create a variable called usersRef that references the Firestore collection named users using firebase.firestore().collection('users').
Firebase
Hint
Use firebase.firestore().collection('users') to get the collection reference.
2
Add a user document with automatic ID using add()
Use the add() method on usersRef to add a new user document with these exact fields: { name: 'Alice', age: 30 }.
Firebase
Hint
Call usersRef.add() with the user data object.
3
Create a user document with a specific ID using set()
Create a document reference called bobDoc inside usersRef with the ID 'bob123' using usersRef.doc('bob123'). Then use set() on bobDoc to set the user data { name: 'Bob', age: 25 }.
Firebase
Hint
Use usersRef.doc('bob123') to get the document reference, then call set() with the data.
4
Complete Firestore document additions
Ensure the full code includes the usersRef collection reference, the add() call for Alice, and the set() call for Bob with ID 'bob123'.
Firebase
Hint
Review your code to confirm all parts are included.
Practice
(1/5)
1. What is the main difference between add and set when adding documents in Firebase?
easy
A. add creates a document with an automatic unique ID, set uses a specified ID.
B. add updates existing documents, set only creates new documents.
C. add deletes documents, set reads documents.
D. add requires a document ID, set generates one automatically.
Solution
Step 1: Understand add behavior
add creates a new document with a unique ID generated by Firebase automatically.
Step 2: Understand set behavior
set creates or updates a document at a specified ID you provide.
Final Answer:
add creates a document with an automatic unique ID, set uses a specified ID. -> Option A
Quick Check:
add auto ID, set specified ID = A [OK]
Hint: Remember: add auto-generates ID, set needs your ID [OK]
Common Mistakes:
Thinking set always creates new documents only
Confusing which method generates IDs automatically
Assuming add can update existing documents
2. Which of the following is the correct syntax to add a new document with an automatic ID in Firebase Firestore?
easy
A. db.collection('users').add({name: 'Alice'})
B. db.collection('users').set({name: 'Alice'})
C. db.collection('users').doc('user1').add({name: 'Alice'})
D. db.collection('users').doc().add({name: 'Alice'})
Solution
Step 1: Identify method for automatic ID
add creates a new document with an automatic ID inside a collection.
Step 2: Check syntax correctness
db.collection('users').add({name: 'Alice'}) is the correct syntax to add a new document with auto ID.
Final Answer:
db.collection('users').add({name: 'Alice'}) -> Option A
Quick Check:
Use add for auto ID = B [OK]
Hint: Use add() on collection to auto-generate document ID [OK]
Common Mistakes:
Using set() without specifying a document ID
Calling add() on a document reference instead of collection