Bird
Raised Fist0
Firebasecloud~15 mins

Migrating from Realtime Database to Firestore in Firebase - Commands & Configuration

Choose your learning style10 modes available
Introduction
Moving your app data from Firebase Realtime Database to Firestore helps you use a more scalable and flexible database. This migration lets you improve data structure and query capabilities without losing your existing data.
When your app needs more advanced queries and indexing than Realtime Database offers
When you want better scalability for large datasets and high traffic
When you want to organize data in collections and documents for easier management
When you want offline support with automatic syncing in a more robust way
When you plan to use Firestore security rules for fine-grained access control
Commands
Log in to your Firebase account to access your projects and use Firebase CLI commands.
Terminal
firebase login
Expected OutputExpected
āœ” Success! Logged in as your-email@example.com
Initialize Firestore in your Firebase project to set up the Firestore database and rules.
Terminal
firebase init firestore
Expected OutputExpected
=== Firestore Setup === Firestore rules file: firestore.rules Firestore indexes file: firestore.indexes.json āœ” Firestore has been initialized in your project.
Export all data from your Realtime Database to a JSON file as a backup before migration.
Terminal
firebase database:get / > realtime_backup.json
Expected OutputExpected
{ "users": {"user1": {"name": "Alice"}}, "messages": {"msg1": {"text": "Hello"}} }
Run a custom Node.js script that reads the Realtime Database JSON backup and writes it to Firestore with the new structure.
Terminal
node migrate_to_firestore.js
Expected OutputExpected
Migration started... Migrated collection users with 1 documents Migrated collection messages with 1 documents Migration completed successfully.
Deploy Firestore indexes defined in firestore.indexes.json to optimize queries after migration.
Terminal
firebase firestore:indexes
Expected OutputExpected
āœ” Firestore indexes deployed successfully.
Key Concept

If you remember nothing else, remember: export your Realtime Database data first, then transform and import it into Firestore carefully to keep data consistent.

Common Mistakes
Trying to migrate data directly without exporting a backup first
You risk losing data or corrupting your database if something goes wrong during migration
Always export your Realtime Database data to a JSON file before starting migration
Not adjusting data structure to Firestore's collections and documents model
Firestore uses a different data model, so flat JSON from Realtime Database may not work well or cause inefficient queries
Transform your data into collections and documents format before importing into Firestore
Skipping Firestore index deployment after migration
Queries may be slow or fail without proper indexes configured
Define and deploy Firestore indexes using firestore.indexes.json after migration
Summary
Log in to Firebase and initialize Firestore in your project.
Export your Realtime Database data to a JSON file as a backup.
Use a script to transform and import the data into Firestore collections and documents.
Deploy Firestore indexes to optimize query performance.

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

  1. Step 1: Understand data structure differences

    Realtime Database organizes data as a single JSON tree, making it less flexible for complex queries.
  2. Step 2: Recognize Firestore's document model

    Firestore stores data in documents grouped into collections, allowing more structured and scalable data management.
  3. Final Answer:

    Realtime Database stores data as one large JSON tree, Firestore stores data in documents and collections. -> Option C
  4. 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

  1. 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.
  2. Step 2: Confirm the syntax

    The command outputs JSON to stdout, which is redirected to ./backup.json.
  3. Final Answer:

    firebase database:get / > ./backup.json -> Option A
  4. Quick Check:

    Export command syntax = firebase database:get / > ./backup.json [OK]
Hint: Export Realtime DB with 'database:get / > ./backup.json' command [OK]
Common Mistakes:
  • Using firestore export command for Realtime Database
  • Incorrect command syntax
  • Confusing backup and export commands
3. Given this snippet migrating data from Realtime Database JSON to Firestore documents, what will be the Firestore document ID for user 'user123'?
const userData = {
  "user123": {"name": "Alice", "age": 30},
  "user456": {"name": "Bob", "age": 25}
};

for (const userId in userData) {
  firestore.collection('users').doc(userId).set(userData[userId]);
}
medium
A. The user's name, e.g., "Alice"
B. A random auto-generated ID
C. "users"
D. "user123"

Solution

  1. Step 1: Analyze the loop and document ID assignment

    The loop uses doc(userId), so the document ID is set explicitly to the key, e.g., "user123".
  2. Step 2: Confirm document ID is not auto-generated

    Since doc(userId) is used, Firestore will not auto-generate an ID.
  3. Final Answer:

    "user123" -> Option D
  4. Quick Check:

    Document ID = userId key = "user123" [OK]
Hint: doc(userId) sets document ID explicitly [OK]
Common Mistakes:
  • Assuming Firestore auto-generates IDs always
  • Confusing collection name with document ID
  • Using user name as ID mistakenly
4. You tried migrating data using a script but Firestore documents are empty after migration. What is the most likely cause?
medium
A. You used the wrong collection name in Firestore.
B. You forgot to await the asynchronous set() calls in the migration script.
C. You exported data from Firestore instead of Realtime Database.
D. You did not enable Firestore in the Firebase console.

Solution

  1. Step 1: Understand asynchronous Firestore writes

    Firestore's set() method returns a promise that must be awaited to complete the write.
  2. Step 2: Identify missing await causes empty documents

    If set() is not awaited, the script may finish before writes complete, resulting in empty documents.
  3. Final Answer:

    You forgot to await the asynchronous set() calls in the migration script. -> Option B
  4. 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

  1. Step 1: Understand Firestore data modeling best practices

    Firestore supports subcollections to model one-to-many relationships efficiently.
  2. Step 2: Choose structure preserving hierarchy and query efficiency

    Storing posts as subcollections under user documents keeps data organized and allows targeted queries.
  3. Final Answer:

    Store users as documents in 'users' collection and posts as subcollections under each user document. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard