-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportSearch.js
More file actions
30 lines (25 loc) · 846 Bytes
/
Copy pathimportSearch.js
File metadata and controls
30 lines (25 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const xlsx = require('xlsx');
const mongoose = require('mongoose');
const md5 = require('md5');
const connectDB = require('./config/db');
const Data = require('./models/QuranSearch');
// Connect to MongoDB
connectDB();
// Read the Excel file
const workbook = xlsx.readFile('Ayat.xlsx');
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = xlsx.utils.sheet_to_json(worksheet);
// Hashing function using MD5
const hashData = (data) => {
return md5(JSON.stringify(data));
};
// Insert the data into MongoDB
Data.insertMany(jsonData.map((data) => ({ ...data, hash: hashData(data) })), (err) => {
if (err) {
console.error('Error importing data:', err);
} else {
console.log('Data imported successfully');
}
// Close the MongoDB connection
mongoose.connection.close();
});