0
0
Firebasecloud~15 mins

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

Choose your learning style9 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.