Skip to content

Instantly share code, notes, and snippets.

@Makiyu-py
Last active June 17, 2021 11:14
Show Gist options
  • Save Makiyu-py/7545a3fb92a541efdfbd463327b8ad2b to your computer and use it in GitHub Desktop.
Save Makiyu-py/7545a3fb92a541efdfbd463327b8ad2b to your computer and use it in GitHub Desktop.
Move all of your collections from different dbs to a specific db on MongoDB using Node.JS! (based on this stackoverflow answer (https://stackoverflow.com/a/54715607/14614326))
// don't forget to init npm first
// npm init -y
// npm install mongodb
const { MongoClient } = require('mongodb');
// Replace the uri string with your MongoDB deployment's connection string.
const uri =
'mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]';
const client = new MongoClient(uri, { options: 'here' });
async function run() {
try {
await client.connect();
async function moveDocuments(sourceCollection, targetCollection, filter) {
const sourceDocs = await sourceCollection.find(filter);
console.log(
`Moving ${await sourceDocs.count()} documents from ${
sourceCollection.collectionName
} to ${targetCollection.collectionName}`
);
const idsOfCopiedDocs = await insertDocuments(
targetCollection,
sourceDocs
);
const targetDocs = await targetCollection.find({
_id: { $in: idsOfCopiedDocs },
});
await deleteDocuments(sourceCollection, targetDocs);
console.log('Done!');
}
async function insertDocuments(collection, documents) {
const insertedIds = [];
const bulkWrites = [];
await documents.forEach((doc) => {
const { _id } = doc;
insertedIds.push(_id);
bulkWrites.push({
replaceOne: {
filter: { _id },
replacement: doc,
upsert: true,
},
});
});
if (bulkWrites.length)
await collection.bulkWrite(bulkWrites, { ordered: false });
return insertedIds;
}
async function deleteDocuments(collection, documents) {
const bulkWrites = [];
await documents.forEach(({ _id }) => {
bulkWrites.push({
deleteOne: {
filter: { _id },
},
});
});
if (bulkWrites.length)
await collection.bulkWrite(bulkWrites, { ordered: false });
}
const dataDB = client.db('Target DB'); // change str to the transferor db name
let datadbColNames = await dataDB.listCollections().toArray();
datadbColNames = datadbColNames.map((x) => x.name);
for (dbName of ['Transferee DB 1', 'Transferee DB 2']) {
let db = client.db(dbName);
let dbColNames = await db.listCollections().toArray();
for (colName of dbColNames.map((x) => x.name)) {
let sourceCol = db.collection(`${colName}`);
if (!datadbColNames.includes(`${colName}`)) {
// make collection if it's not on target db
await dataDB.createCollection(`${colName}`);
}
let targetCol = dataDB.collection(`${colName}`);
// change the last param if you only want specific posts to be transfered
await moveDocuments(sourceCol, targetCol, {});
}
}
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch((err) => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment