-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteRandomFemales.js
33 lines (30 loc) · 1.25 KB
/
deleteRandomFemales.js
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
const mongoose = require('mongoose');
const User = require('./models/User'); // Update with correct path to your User model
require('dotenv').config();
// Connect to MongoDB
const mongoURI = process.env.MONGO_URI; // Replace with your MongoDB connection string
mongoose.connect(mongoURI, { useUnifiedTopology: true })
.then(() => {
console.log('MongoDB connected successfully.');
deleteRandomFemaleUsers(500); // Delete 54 random female users
})
.catch((err) => console.error('MongoDB connection error:', err));
// Function to delete random female users
async function deleteRandomFemaleUsers(numToDelete) {
try {
// Find random female users
const females = await User.find({ gender: 'female' }).limit(numToDelete).lean();
// Check if we have enough users to delete
if (females.length < numToDelete) {
console.log(`Only found ${females.length} female users to delete.`);
} else {
// Delete the found female users
await User.deleteMany({ _id: { $in: females.map(user => user._id) } });
console.log(`Successfully deleted ${numToDelete} random female users.`);
}
} catch (error) {
console.error('Error deleting female users:', error);
} finally {
mongoose.connection.close(); // Close the connection
}
}