50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
const { MongoClient } = require('mongodb');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const MONGODB_URI = 'mongodb://localhost/beanstalk';
|
|
const OUTPUT_DIR = path.join(__dirname, '../public/db');
|
|
|
|
async function dumpDatabase() {
|
|
const client = new MongoClient(MONGODB_URI);
|
|
|
|
try {
|
|
console.log('Connecting to MongoDB...');
|
|
await client.connect();
|
|
|
|
const db = client.db();
|
|
|
|
// Create output directory if it doesn't exist
|
|
if (!fs.existsSync(OUTPUT_DIR)) {
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
}
|
|
|
|
console.log('Getting collections...');
|
|
const collections = await db.listCollections().toArray();
|
|
|
|
for (const collection of collections) {
|
|
const collectionName = collection.name;
|
|
console.log(`Dumping collection: ${collectionName}`);
|
|
|
|
const data = await db.collection(collectionName).find({}).toArray();
|
|
|
|
// Write to JSON file
|
|
const filePath = path.join(OUTPUT_DIR, `${collectionName}.json`);
|
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
|
|
console.log(` ✓ ${data.length} documents saved to ${filePath}`);
|
|
}
|
|
|
|
console.log('\nDatabase dump completed successfully!');
|
|
console.log(`Output directory: ${OUTPUT_DIR}`);
|
|
|
|
} catch (error) {
|
|
console.error('Error dumping database:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
}
|
|
|
|
dumpDatabase();
|