seed the mongo data
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
const { MongoClient } = require('mongodb');
|
||||
const fs = require('fs');
|
||||
|
||||
const url = 'mongodb://root:root@localhost:27017/?authSource=admin';
|
||||
const dbName = 'magic-stream-movies';
|
||||
|
||||
async function importJson(collectionName, jsonFilePath) {
|
||||
const client = new MongoClient(url);
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
console.log('Connected to MongoDB successfully.');
|
||||
|
||||
const db = client.db(dbName);
|
||||
const collection = db.collection(collectionName);
|
||||
|
||||
const rawData = fs.readFileSync(jsonFilePath, 'utf-8');
|
||||
const jsonData = JSON.parse(rawData);
|
||||
|
||||
const documents = Array.isArray(jsonData) ? jsonData : [jsonData];
|
||||
|
||||
const result = await collection.insertMany(documents);
|
||||
console.log(`${result.insertedCount} documents successfully imported!`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('An error occurred during the import:', error);
|
||||
} finally {
|
||||
await client.close();
|
||||
}
|
||||
}
|
||||
|
||||
importJson('users', './users.json');
|
||||
importJson('genres', './genres.json');
|
||||
importJson('rankings', './rankings.json');
|
||||
importJson('movies', './movies.json');
|
||||
Reference in New Issue
Block a user