-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcleanup-blank-notes.js
More file actions
66 lines (55 loc) · 2.25 KB
/
cleanup-blank-notes.js
File metadata and controls
66 lines (55 loc) · 2.25 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
const mongoose = require('mongoose');
require('dotenv').config();
// Import Note model
const Note = require('./src/models/Note');
async function cleanupBlankNotes() {
try {
console.log('Connecting to MongoDB...');
await mongoose.connect(process.env.MONGODB_URI);
console.log('Connected to MongoDB');
// Find notes with empty or whitespace-only content
const blankNotes = await Note.find({
$or: [
{ content: '' },
{ content: /^\s*$/ }, // Only whitespace
{ content: { $exists: false } }
]
});
console.log(`Found ${blankNotes.length} blank notes`);
if (blankNotes.length > 0) {
// Show some examples
console.log('Sample blank notes:');
blankNotes.slice(0, 5).forEach(note => {
console.log(`- URL: ${note.url}, Created: ${note.createdAt}, Content: "${note.content}"`);
});
// Ask for confirmation (in a real environment, you'd want user input)
console.log('\nTo delete these notes, uncomment the delete line below and run again.');
// Uncomment the line below to actually delete the blank notes
// const result = await Note.deleteMany({
// $or: [
// { content: '' },
// { content: /^\s*$/ },
// { content: { $exists: false } }
// ]
// });
// console.log(`Deleted ${result.deletedCount} blank notes`);
} else {
console.log('No blank notes found');
}
// Show stats
const totalNotes = await Note.countDocuments();
const notesWithContent = await Note.countDocuments({
content: { $exists: true, $ne: '', $not: /^\s*$/ }
});
console.log(`\nDatabase stats:`);
console.log(`Total notes: ${totalNotes}`);
console.log(`Notes with content: ${notesWithContent}`);
console.log(`Blank notes: ${totalNotes - notesWithContent}`);
} catch (error) {
console.error('Error:', error);
} finally {
await mongoose.disconnect();
console.log('Disconnected from MongoDB');
}
}
cleanupBlankNotes();