Skip to content

Instantly share code, notes, and snippets.

@ultimagriever
Created August 26, 2016 15:33
Show Gist options
  • Save ultimagriever/e2a60d348fe71c3a5d7ab461fc56fce4 to your computer and use it in GitHub Desktop.
Save ultimagriever/e2a60d348fe71c3a5d7ab461fc56fce4 to your computer and use it in GitHub Desktop.
Migrate Mongo -> Firebase
/**
* Migrate MongoDB database to Firebase.
* Syntax: node migrate.js [-c || --collection=]<MongoDB Collection> [ [-h || --host=]<MongoDB URI>]
* If no args are provided, the MONGODB_URI
* env variable declared in the .env file
* will be used instead.
*/
const env = require('dotenv');
const firebase = require('firebase');
const execSync = require('child_process').execSync;
const minimist = require('minimist');
const readline = require('readline');
const fs = require('fs');
const args = minimist(process.argv.slice(2));
env.config({ silent: true });
var collection = args.c || args.collection;
// Parse MongoDB URI - if none is provided, get the environment URI instead
var mongoURI = args.h || args.host || process.env.MONGODB_URI;
// Remove mongo protocol from URI
mongoURI = mongoURI.substr(10);
// Separate data from URI - split on "@", "/" (escaped) and ":" (also escaped)
var mongoData = mongoURI.split(/[@\/\:]/);
// Convert mongoData to object/dictionary
var keys = ["user", "password", "uri", "port", "database"];
keys.forEach((keys, i) => {
mongoData[keys] = mongoData[0];
mongoData.shift(); // Removing number-indexed values
});
var command = "mongoexport -h " + mongoData.uri + ":" + mongoData.port +
" -d " + mongoData.database + " -u " + mongoData.user +
" -p " + mongoData.password + " -c " + collection + " -o backup.json";
console.log("=>", command);
// Run mongoexport -h <uri>:<port> -u <username> -p <password> -d <database> -c <collection>
var child = execSync(command);
var backup = [];
var lines = fs.readFileSync('backup.json').toString().split('\n');
lines.pop(); // If last element length is lesser than 1, pop from array
// Assign parsed JSON to backup var
lines.forEach(line => {
backup.push(JSON.parse(line));
});
// Remove _id field, convert data objects to string if they are objects
backup = backup.map(item => {
delete item._id;
if (typeof item.endDateTime === "object") {
item.endDateTime = item.endDateTime["$date"];
}
if (typeof item.createDate === "object") {
item.createDate = item.createDate["$date"];
}
if (typeof item.updateDate === "object") {
item.updateDate = item.updateDate["$date"];
}
return item;
});
// Initialize Firebase
var config = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.FIREBASE_DATABASE_URL,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET
};
const firebaseApp = firebase.initializeApp(config);
// Initialize database
const database = firebaseApp.database().ref().child(collection);
var promises = [];
// Insert backup data
backup.forEach(row => {
promises.push(database.push(row));
});
Promise.all(promises).then(() => {
process.exit(1);
});
@frbuccoliero
Copy link

Is this still working in 2019?

@jjosserand
Copy link

I would also like to know if it still works today, please and urgently if you please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment