Transfer data from Cloud Firestore to MongoDB

Anton Masanavets
1 min readApr 24, 2021

When my project got 100.000+ users i accepted decision for migrate from firebase to noSql data base. It’s simple instuction how transfer data to mongodb from firebase. But, we are not will be touching nodejs server creation, API, authorisation and etc.

So, first we need create JSON data file from cloud firebase

npm install @sgg10/firestore-backup

const firestoreBackup = require('@sgg10/firestore-backup');
const serviceAccount = require('./key');

let fsb = new firestoreBackup(serviceAccount, 'https://you data base link here');
// fsb.exportAll().then(result => fsb.saveFile(result, { name: 'All'}));
fsb.exportCustom(['users']).then(result => fsb.saveFile(result, { name: 'users'}));

Next, we have json file, so, parse this file and save to mongodb as one docs

npm install mongodb — save

const { MongoClient } = require('mongodb');
const jsonFile = require('./users.json');

const MONGO_USERNAME = '';
const MONGO_PASSWORD = '';
const MONGO_HOSTNAME = '';
const MONGO_PORT = '27017';
const MONGO_DB = '';
const AUTH_SOURCE = '';

const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=${AUTH_SOURCE}`;
const client = new MongoClient(url, { useUnifiedTopology: true });

const start = async () => {
await client.connect()
.then(() => console.log('success'))
.catch(() => console.log('error'))
}

const saveToDb = () => {
const database = client.db(MONGO_DB);
const users = database.collection("users");
const userKeys = Object.keys(jsonFile.users);
for (let i = 0; i < userKeys.length; i++) {
users.insertOne({ _id: userKeys[i], ...jsonFile.users[userKeys[i]] })
}
}
start().then(() => {
saveToDb();
})

it is all, we have 100.000 docs in collection mongoDb

--

--