📖 Scenario: You have a Firebase Realtime Database storing user profiles. You want to move this data to Firestore for better querying and scalability.
🎯 Goal: Build a simple Firebase script that reads user data from Realtime Database and writes it into Firestore with the same structure.
📋 What You'll Learn
Create a reference to the Realtime Database users node
Create a Firestore collection reference for users
Read all user data from Realtime Database
Write each user document into Firestore
💡 Why This Matters
🌍 Real World
Many Firebase projects start with Realtime Database and later migrate to Firestore for advanced querying and scalability.
💼 Career
Understanding how to migrate data between Firebase databases is useful for cloud engineers and developers working with Firebase backend services.
Progress0 / 4 steps
1
Setup Firebase Realtime Database reference
Create a variable called rtdbRef that references the path 'users' in the Firebase Realtime Database using firebase.database().ref('users').
Firebase
Hint
Use firebase.database().ref('users') to get the reference.
2
Setup Firestore collection reference
Create a variable called firestoreUsers that references the 'users' collection in Firestore using firebase.firestore().collection('users').
Firebase
Hint
Use firebase.firestore().collection('users') to get the Firestore collection reference.
3
Read data from Realtime Database and prepare for migration
Use rtdbRef.once('value') to read all user data from Realtime Database. Inside the callback, get the snapshot value into a variable called usersData.
Firebase
Hint
Use once('value') and snapshot.val() to get the data.
4
Write each user to Firestore
Inside the rtdbRef.once('value').then() callback, use a for...in loop with variable userId to iterate over usersData. For each userId, write the user data to Firestore using firestoreUsers.doc(userId).set(usersData[userId]).
Firebase
Hint
Use a for...in loop and firestoreUsers.doc(userId).set() to write data.
Practice
(1/5)
1. What is the main difference between Firebase Realtime Database and Firestore when migrating data?
easy
A. Realtime Database requires manual backups, Firestore automatically backs up data.
B. Realtime Database uses SQL queries, Firestore uses NoSQL queries.
C. Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections.
D. Realtime Database supports offline mode, Firestore does not.
Solution
Step 1: Understand data structure differences
Realtime Database organizes data as a single JSON tree, making it less flexible for complex queries.
Step 2: Recognize Firestore's document model
Firestore stores data in documents grouped into collections, allowing more structured and scalable data management.
Final Answer:
Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections. -> Option C
Quick Check:
Data structure difference = Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections. [OK]
Hint: Remember: JSON tree vs documents/collections [OK]
Common Mistakes:
Confusing query languages between databases
Assuming Firestore lacks offline support
Thinking backups are automatic in Realtime Database
2. Which of the following is the correct Firebase CLI command to export data from Realtime Database before migration?
easy
A. firebase database:get / > ./backup.json
B. firebase firestore:export ./backup
C. firebase database:backup ./backup.json
D. firebase export realtime ./backup.json
Solution
Step 1: Identify the correct CLI command for Realtime Database export
The Firebase CLI uses firebase database:get / > ./backup.json to export Realtime Database data.
Step 2: Confirm the syntax
The command outputs JSON to stdout, which is redirected to ./backup.json.
Final Answer:
firebase database:get / > ./backup.json -> Option A
If set() is not awaited, the script may finish before writes complete, resulting in empty documents.
Final Answer:
You forgot to await the asynchronous set() calls in the migration script. -> Option B
Quick Check:
Await async writes = You forgot to await the asynchronous set() calls in the migration script. [OK]
Hint: Always await Firestore writes in migration scripts [OK]
Common Mistakes:
Ignoring async nature of Firestore writes
Assuming collection name mismatch causes empty docs
Confusing export source databases
5. You want to migrate nested Realtime Database data with users and their posts to Firestore. Which approach best preserves the data structure and supports efficient queries?
hard
A. Store users as documents in 'users' collection and posts as subcollections under each user document.
B. Flatten all data into a single collection with combined user-post IDs as document IDs.
C. Store all data in one large Firestore document per user including all posts as nested arrays.
D. Migrate posts to a separate collection without linking to users.
Solution
Step 1: Understand Firestore data modeling best practices
Firestore supports subcollections to model one-to-many relationships efficiently.
Step 2: Choose structure preserving hierarchy and query efficiency
Storing posts as subcollections under user documents keeps data organized and allows targeted queries.
Final Answer:
Store users as documents in 'users' collection and posts as subcollections under each user document. -> Option A
Quick Check:
Use subcollections for nested data = Store users as documents in 'users' collection and posts as subcollections under each user document. [OK]
Hint: Use subcollections for nested related data in Firestore [OK]
Common Mistakes:
Putting all data in one large document causing size limits
Flattening data losing natural hierarchy
Separating posts without user linkage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.