How to export all collections in MongoDB?
mongodump --archive="my-local-db" --db=my
mongorestore --archive="my-local-db" --nsFrom='my.*' --nsTo='mynew.*'
How to export all collections in MongoDB?
mongodump --archive="my-local-db" --db=my
mongorestore --archive="my-local-db" --nsFrom='my.*' --nsTo='mynew.*'
nodejs export all mongodb collections
const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const dbName = 'database1';
const client = new MongoClient('db_url', {useUnifiedTopology:true,useNewUrlParser: true, });
client.connect(function(err) {
console.log('Connected successfully to server');
const db = client.db(dbName);
var collections = [ 'collection1', 'collection2' ];
collections.forEach(async collection => {
var documents = await getDocuments(db, collection);
try {
// Write files outside of server directory
// prevents app restarts on nodemon
fs.writeFile("../"+collection+'.json', JSON.stringify(docus), err => {
});
console.log('Done writing to file.');
} catch (err) {
console.log('Error writing to file', err)
}
})
});
async function getDocuments(db, collection) {
return await db.collection(collection).find({}).toArray()
};
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us