-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateCourses.js
More file actions
61 lines (51 loc) · 1.79 KB
/
Copy pathupdateCourses.js
File metadata and controls
61 lines (51 loc) · 1.79 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
import mongoose from "mongoose";
import "dotenv/config";
import model from "./Kambaz/Courses/model.js";
const CONNECTION_STRING = process.env.DATABASE_CONNECTION_STRING || "mongodb://127.0.0.1:27017/kambaz";
async function updateCourses() {
try {
console.log("Connecting to MongoDB...");
await mongoose.connect(CONNECTION_STRING);
console.log("Connected to MongoDB");
// Find all courses without name or description
const courses = await model.find({
$or: [
{ name: { $exists: false } },
{ name: null },
{ name: "" },
{ description: { $exists: false } },
{ description: null },
{ description: "" }
]
});
console.log(`Found ${courses.length} courses that need updating`);
if (courses.length === 0) {
console.log("All courses already have name and description!");
await mongoose.connection.close();
process.exit(0);
}
// Update each course with default values if missing
for (const course of courses) {
const updates = {};
if (!course.name || course.name === "") {
updates.name = `Course ${course._id.substring(0, 8)}`;
}
if (!course.description || course.description === "") {
updates.description = "No description available";
}
if (Object.keys(updates).length > 0) {
await model.updateOne({ _id: course._id }, { $set: updates });
console.log(`Updated course ${course._id}:`, updates);
}
}
console.log(`\nSuccessfully updated ${courses.length} courses`);
await mongoose.connection.close();
console.log("Database update completed!");
process.exit(0);
} catch (error) {
console.error("Error updating courses:", error);
await mongoose.connection.close();
process.exit(1);
}
}
updateCourses();