In this article, we migrate existing data from the sourced database (MongoDB) to the destination database (MongoDB) using Node.js. To perform the whole operation, we will first insert documents into the sourced database and then migrate to the destination.
Approach to Perform Data Migration in MongoDB using Node.js- Project Structure: Create a root directory named “MongoDB-data-migration”.
- Set Up Mongoose: Install required modules like Express and Mongoose and connect to the database.
- Schema & Model: Define the Schema (source, destination) and Model (source, destination ).
- Data Insertion: Insert data into Source collection using the insert function.
- Migrate Data: Migrate data from Sourced database to Destination database.
Steps to Create an ApplicationStep 1. Make a folder named ‘MongoDB-data-migration’ and navigate to it using this command.
mkdir mongodb-data-migration cd mongodb-data-migration Step 2: Install required modules like Mongoose and Express.
npm i mongoose express Project Structure:.png) Project Structure Updated dependencies in package.json file"dependencies": { "express": "^4.19.2", "mongoose": "^8.4.0" } Example: This code is inserting data into sourced database.
JavaScript
// server.js
const mongoose = require('mongoose')
const dbConnection = (URI) => {
mongoose.connect(URI)
console.log('Database is connected Successfully.')
}
const sourceSchema = new mongoose.Schema({
name: String,
emailId: String,
age: Number
})
const SourceModel = mongoose.model('SourceModel', sourceSchema)
module.exports = {
dbConnection,
SourceModel
}
JavaScript
// app.js
const express = require('express')
const app = express();
const { SourceModel, dbConnection } = require('./server')
const MONGODB_SOURCE_URI = 'xxxx/Source'
const PORT = 8000;
const userData = [
{
name: 'Aarav Sharma',
email: '[email protected]',
age: 28
},
{
name: 'Vihaan Mehta',
email: '[email protected]',
age: 35
},
{
name: 'Aditya Patel',
email: '[email protected]',
age: 30
},
{
name: 'Ananya Roy',
email: '[email protected]',
age: 25
},
{
name: 'Aadhya Joshi',
email: '[email protected]',
age: 24
},
{
name: 'Diya Nair',
email: '[email protected]',
age: 23
},
{
name: 'Myra Iyer',
email: '[email protected]',
age: 32
},
{
name: 'Anika Prasad',
email: '[email protected]',
age: 28
},
{
name: 'Saanvi Menon',
email: '[email protected]',
age: 29
},
{
name: 'Aarohi Chatterjee',
email: '[email protected]',
age: 26
},
{
name: 'Prisha Bhatt',
email: '[email protected]',
age: 30
}
]
async function insertingDoc(userData) {
try {
dbConnection(MONGODB_SOURCE_URI)
const result = await SourceModel.insertMany(userData);
console.log('Data is inserted Successfully.', res)
} catch (error) {
console.log('Something went wrong.')
}
}
insertingDoc(userData)// calling
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
})
Output :
 Inserted document Output Example: This code migrate data from sourced database to destination database.
JavaScript
// app.js
const express = require('express')
const app = express();
const { dbConnection, dbDisconnection, SourceModel, DestModel } = require('./server')
const PORT = 8000;
const MONGODB_SOURCE_URI = 'xxxx/Source'
const MONGODB_DESTINATION_URI = 'xxxx/Destination'
async function dataMigration() {
try {
// connecting to sourced database
await dbConnection(MONGODB_SOURCE_URI)
const sourceData = await SourceModel.find().lean();
// disconnecting to sourced database
await dbDisconnection();
// now connecting to destination database
await dbConnection(MONGODB_DESTINATION_URI)
await DestModel.insertMany(sourceData)
console.log('Data Migration is Successfull.')
} catch (error) {
console.log('Something went wrong.', error)
return;
}
}
dataMigration()//
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
})
JavaScript
// server.js
const mongoose = require('mongoose')
const dbConnection = async (URI) => {
await mongoose.connect(URI)
console.log('Database is connected Successfully.')
}
const dbDisconnection = async () => {
await mongoose.disconnect()
console.log('Database is Disconnected Successfully.')
}
const sourceSchema = new mongoose.Schema({
name: String,
emailId: String,
age: Number
})
const destSchema = new mongoose.Schema({
name: String,
emailId: String,
age: Number
})
const SourceModel = mongoose.model('SourceModel', sourceSchema)
const DestModel = mongoose.model('DestModel', destSchema)
module.exports = {
dbConnection,
dbDisconnection,
SourceModel,
DestModel
}
Output:
 Data Migration Output Output:
|