-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcleanup-database.js
More file actions
99 lines (81 loc) · 3.54 KB
/
cleanup-database.js
File metadata and controls
99 lines (81 loc) · 3.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require("dotenv").config();
const mongoose = require("mongoose");
const Listing = require("./models/listing.js");
const Booking = require("./models/booking.js");
const MONGO_URL = "mongodb://127.0.0.1:27017/wanderlust";
async function cleanupDatabase() {
try {
// Connect to MongoDB
await mongoose.connect(MONGO_URL);
console.log("✅ Connected to MongoDB");
// Find listings with missing essential data
const incompleteListings = await Listing.find({
$or: [
{ title: { $exists: false } },
{ title: "" },
{ title: null },
{ price: { $exists: false } },
{ price: null },
{ price: { $lte: 0 } },
{ location: { $exists: false } },
{ location: "" },
{ location: null },
{ "image.url": { $exists: false } },
{ "image.url": "" },
{ "image.url": null }
]
});
console.log(`\n🔍 Found ${incompleteListings.length} incomplete listings:`);
if (incompleteListings.length > 0) {
// Show details of incomplete listings
incompleteListings.forEach((listing, index) => {
console.log(`\n${index + 1}. ID: ${listing._id}`);
console.log(` Title: ${listing.title || 'MISSING'}`);
console.log(` Price: ${listing.price || 'MISSING'}`);
console.log(` Location: ${listing.location || 'MISSING'}`);
console.log(` Image URL: ${listing.image?.url || 'MISSING'}`);
});
// Ask for confirmation before deletion
console.log(`\n⚠️ WARNING: This will delete ${incompleteListings.length} incomplete listings!`);
console.log("📋 These listings are missing essential data (title, price, location, or image)");
// For safety, let's create a backup query first
console.log("\n🔄 Proceeding with cleanup...");
// Get the IDs of listings to delete
const listingIdsToDelete = incompleteListings.map(listing => listing._id);
// Step 1: Remove bookings that reference these incomplete listings
const bookingsToUpdate = await Booking.find({
listing: { $in: listingIdsToDelete }
});
if (bookingsToUpdate.length > 0) {
console.log(`\n📅 Found ${bookingsToUpdate.length} bookings referencing incomplete listings`);
console.log("🗑️ Removing these booking references...");
await Booking.deleteMany({
listing: { $in: listingIdsToDelete }
});
console.log("✅ Removed booking references to incomplete listings");
}
// Step 2: Delete the incomplete listings
const deleteResult = await Listing.deleteMany({
_id: { $in: listingIdsToDelete }
});
console.log(`\n✅ Successfully deleted ${deleteResult.deletedCount} incomplete listings`);
console.log("🧹 Database cleanup completed!");
} else {
console.log("✨ No incomplete listings found. Your database is clean!");
}
// Show summary of remaining listings
const remainingListings = await Listing.countDocuments();
console.log(`\n📊 Summary:`);
console.log(` - Remaining listings: ${remainingListings}`);
console.log(` - Deleted listings: ${incompleteListings.length}`);
} catch (error) {
console.error("❌ Error during cleanup:", error);
} finally {
await mongoose.connection.close();
console.log("\n🔌 Disconnected from MongoDB");
}
}
// Run the cleanup
console.log("🧹 Starting database cleanup...");
console.log("🎯 Target: Remove listings with missing essential data (title, price, location, image)");
cleanupDatabase();