v1
This commit is contained in:
68
import-to-mongodb.js
Normal file
68
import-to-mongodb.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const { MongoClient } = require('mongodb');
|
||||
const fs = require('fs');
|
||||
|
||||
// MongoDB connection string - update with your actual connection details
|
||||
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost/beanstalk';
|
||||
const DATABASE_NAME = 'beanstalk';
|
||||
|
||||
async function importJSONToMongoDB() {
|
||||
const client = new MongoClient(MONGODB_URI);
|
||||
|
||||
try {
|
||||
// Connect to MongoDB
|
||||
await client.connect();
|
||||
console.log('Connected to MongoDB');
|
||||
|
||||
const db = client.db(DATABASE_NAME);
|
||||
|
||||
// Import indicators
|
||||
console.log('Importing indicators...');
|
||||
const indicatorsData = JSON.parse(fs.readFileSync('./indicators.json', 'utf8'));
|
||||
|
||||
if (indicatorsData.data && Array.isArray(indicatorsData.data)) {
|
||||
// Clear existing collection
|
||||
await db.collection('indicators').deleteMany({});
|
||||
console.log('Cleared existing indicators collection');
|
||||
|
||||
// Insert new data
|
||||
const result = await db.collection('indicators').insertMany(indicatorsData.data);
|
||||
console.log(`Inserted ${result.insertedCount} indicators`);
|
||||
} else {
|
||||
console.log('No valid data found in indicators.json');
|
||||
}
|
||||
|
||||
// Import learning areas
|
||||
console.log('Importing learning areas...');
|
||||
const learningAreasData = JSON.parse(fs.readFileSync('./learning_areas.json', 'utf8'));
|
||||
|
||||
if (learningAreasData.data && Array.isArray(learningAreasData.data)) {
|
||||
// Clear existing collection
|
||||
await db.collection('learning_areas').deleteMany({});
|
||||
console.log('Cleared existing learning_areas collection');
|
||||
|
||||
// Insert new data
|
||||
const result = await db.collection('learning_areas').insertMany(learningAreasData.data);
|
||||
console.log(`Inserted ${result.insertedCount} learning areas`);
|
||||
} else {
|
||||
console.log('No valid data found in learning_areas.json');
|
||||
}
|
||||
|
||||
console.log('Import completed successfully!');
|
||||
|
||||
// Verify the import
|
||||
const indicatorsCount = await db.collection('indicators').countDocuments();
|
||||
const learningAreasCount = await db.collection('learning_areas').countDocuments();
|
||||
|
||||
console.log(`Verification: ${indicatorsCount} indicators in database`);
|
||||
console.log(`Verification: ${learningAreasCount} learning areas in database`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error during import:', error);
|
||||
} finally {
|
||||
await client.close();
|
||||
console.log('MongoDB connection closed');
|
||||
}
|
||||
}
|
||||
|
||||
// Run the import
|
||||
importJSONToMongoDB();
|
||||
Reference in New Issue
Block a user