Firebase

How to migrate data from Firebase Realtime Database to Firestore?

Discover how to effortlessly transfer data from Firebase Realtime Database to Firestore. Get tips, tools, and best practices for a hassle-free migration.

Developer profile skeleton
a developer thinking

Overview

The process of migrating data from Firebase Realtime Database to Firestore is multi-faceted with several important steps for a smooth transition. For starters, grasping the structural differences is crucial—Firestore uses a document-based model, while Realtime Database relies on a JSON-tree format. Mapping out how data will be converted is vital. After that, set up Firestore and create the needed collections and documents. Efficient data transfer can be achieved using scripts or Firebase functions. Don't forget to update your application code to work with Firestore instead of the Realtime Database. Proper and thorough testing is critical to ensure data integrity and that everything functions correctly.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to migrate data from Firebase Realtime Database to Firestore?

Step 1: Set Up Firebase Project

  • Make sure both Firebase Realtime Database and Firestore are turned on in the Firebase console.
  • Head over to the Firebase console.
  • Pick your project.
  • If Firestore isn't enabled yet, go to Firestore Database and click “Create database.”

Step 2: Initialize Firebase SDK

  • Install the Firebase SDKs in your development environment.
npm install firebase
  • Set up Firebase in your app.
import firebase from "firebase/app";
import "firebase/database";
import "firebase/firestore";

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_AUTH_DOMAIN",
  databaseURL: "YOUR_DATABASE_URL",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_STORAGE_BUCKET",
  messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
  appId: "YOUR_APP_ID"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
const firestore = firebase.firestore();

Step 3: Read Data from Firebase Realtime Database

  • Create a function to read data from Firebase Realtime Database.
async function readFromRealtimeDatabase() {
  const snapshot = await db.ref('/').once('value');
  return snapshot.val();
}

Step 4: Write Data to Firestore

  • Create a function to write data to Firestore.
async function writeToFirestore(data) {
  const batch = firestore.batch();
  
  Object.keys(data).forEach(key => {
    const ref = firestore.collection('your-collection').doc(key);
    batch.set(ref, data[key]);
  });
  
  await batch.commit();
}

Step 5: Migrate Data

  • Combine the read and write functions to migrate the data.
async function migrateData() {
  const data = await readFromRealtimeDatabase();
  await writeToFirestore(data);
}

migrateData().then(() => console.log("Data migration completed!"));

Step 6: Verify Migration

  • Check your Firestore database in the Firebase console to make sure the data has been successfully migrated.

Step 7: Update Your Application Code

  • Change your app's database interaction logic to use Firestore instead of Firebase Realtime Database.
// Example for reading data from Firestore
firestore.collection('your-collection').get()
  .then(querySnapshot => {
    querySnapshot.forEach(doc => {
      console.log(doc.id, " => ", doc.data());
    });
  });

Step 8: Remove Firebase Realtime Database Usage

  • Once you've verified the migration and updated your app logic, remove any remaining Firebase Realtime Database references from your codebase.

Explore more Firebase tutorials

Complete Guide to Firebase: Tutorials, Tips, and Best Practices

Explore our Firebase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.