mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-21 16:25:55 +00:00
add scripts
This commit is contained in:
parent
eb6445b0f9
commit
c298a22257
1 changed files with 45 additions and 0 deletions
45
scripts/removeDuplicate.js
Normal file
45
scripts/removeDuplicate.js
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
|
const TriviaQuestion = require("../models/TriviaQuestion");
|
||||||
|
|
||||||
|
// Connect to MongoDB
|
||||||
|
mongoose
|
||||||
|
.connect(process.env.MONGODB_URI)
|
||||||
|
.then(() => console.log("✅ Connected to MongoDB"))
|
||||||
|
.catch((err) => console.error("❌ Failed to connect to MongoDB", err));
|
||||||
|
|
||||||
|
async function removeDuplicates() {
|
||||||
|
try {
|
||||||
|
// Aggregate duplicates
|
||||||
|
const duplicates = await TriviaQuestion.aggregate([
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: "$question", // Group by the question text
|
||||||
|
count: { $sum: 1 },
|
||||||
|
ids: { $push: "$_id" }, // Collect ids of the duplicate entries
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$match: { count: { $gt: 1 } }, // Match groups with more than one document
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Remove duplicates
|
||||||
|
for (const doc of duplicates) {
|
||||||
|
const idsToRemove = doc.ids.slice(1); // Keep the first entry and remove the rest
|
||||||
|
await TriviaQuestion.deleteMany({ _id: { $in: idsToRemove } });
|
||||||
|
console.log(`Removed duplicates: ${idsToRemove}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Duplicates removed");
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error removing duplicates:", err);
|
||||||
|
} finally {
|
||||||
|
// Close the connection
|
||||||
|
mongoose.connection.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
removeDuplicates();
|
Loading…
Reference in a new issue