-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
61 lines (51 loc) · 2.18 KB
/
upload.js
File metadata and controls
61 lines (51 loc) · 2.18 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import mongoose from 'mongoose';
import dotenv from "dotenv";
dotenv.config();
// MongoDB configuration
const MONGODB_URI = process.env.MONGODB_URI;
const DATABASE_NAME = process.env.DATABASE_NAME;
// Books data
let books = {
1: { "author": "Chinua Achebe", "title": "Things Fall Apart", "reviews": {} },
2: { "author": "Hans Christian Andersen", "title": "Fairy tales", "reviews": {} },
3: { "author": "Dante Alighieri", "title": "The Divine Comedy", "reviews": {} },
4: { "author": "Unknown", "title": "The Epic Of Gilgamesh", "reviews": {} },
5: { "author": "Unknown", "title": "The Book Of Job", "reviews": {} },
6: { "author": "Unknown", "title": "One Thousand and One Nights", "reviews": {} },
7: { "author": "Unknown", "title": "Nj\u00e1l's Saga", "reviews": {} },
8: { "author": "Jane Austen", "title": "Pride and Prejudice", "reviews": {} },
9: { "author": "Honor\u00e9 de Balzac", "title": "Le P\u00e8re Goriot", "reviews": {} },
10: { "author": "Samuel Beckett", "title": "Molloy, Malone Dies, The Unnamable, the trilogy", "reviews": {} }
};
// Define the book schema and model
const bookSchema = new mongoose.Schema({
author: { type: String, required: true },
title: { type: String, required: true },
reviews: { type: Object, default: {} }
});
const Book = mongoose.model('Book', bookSchema);
async function uploadBooks() {
try {
// Connect to MongoDB
await mongoose.connect(MONGODB_URI, {
dbName: DATABASE_NAME,
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Connected to MongoDB');
// Clear the existing books collection
await Book.deleteMany({});
console.log('Existing books cleared');
// Upload new books
const booksArray = Object.values(books); // Convert the books object to an array
await Book.insertMany(booksArray);
console.log('Books uploaded successfully');
// Close the connection
await mongoose.connection.close();
console.log('Connection closed');
} catch (error) {
console.error('Error uploading books:', error.message);
}
}
// Run the function
uploadBooks();