0
0
Firebasecloud~15 mins

Setting with merge option in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Firestore: Setting Document Data with Merge Option
📖 Scenario: You are building a simple app that stores user profiles in Firebase Firestore. Sometimes you want to update only part of a user's profile without overwriting the entire document.
🎯 Goal: Learn how to use the set method with the merge: true option to update parts of a Firestore document safely.
📋 What You'll Learn
Create a Firestore document reference for a user with ID 'user123'.
Create an initial user profile object with name and age.
Use set to save the initial profile to Firestore.
Create an update object with a new age and city.
Use set with { merge: true } to update the user profile without deleting existing fields.
💡 Why This Matters
🌍 Real World
Updating user profiles or settings in Firestore without deleting existing data is common in apps.
💼 Career
Understanding how to safely update Firestore documents is essential for backend and full-stack developers working with Firebase.
Progress0 / 4 steps
1
Create Firestore document reference and initial user profile
Create a variable called userRef that references the Firestore document at users/user123. Then create a variable called initialProfile with the exact object { name: 'Alice', age: 30 }.
Firebase
Need a hint?

Use firestore.collection('users').doc('user123') to get the document reference.

2
Save the initial profile to Firestore
Use userRef.set(initialProfile) to save the initial profile object to Firestore.
Firebase
Need a hint?

Call set on userRef with initialProfile as argument.

3
Create an update object with new age and city
Create a variable called updateProfile with the exact object { age: 31, city: 'New York' }.
Firebase
Need a hint?

Define updateProfile with the new age and city fields.

4
Update the user profile using set with merge option
Use userRef.set(updateProfile, { merge: true }) to update the Firestore document without overwriting the entire profile.
Firebase
Need a hint?

Call set on userRef with updateProfile and the option { merge: true }.