0
0
Firebasecloud~30 mins

Migrating from Realtime Database to Firestore in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Migrating from Realtime Database to Firestore
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use a for...in loop and firestoreUsers.doc(userId).set() to write data.