Skip to content

Instantly share code, notes, and snippets.

@polsala
Created April 15, 2025 11:24
Show Gist options
  • Select an option

  • Save polsala/4fba1fb7be823354f60d52ea2de8cdd1 to your computer and use it in GitHub Desktop.

Select an option

Save polsala/4fba1fb7be823354f60d52ea2de8cdd1 to your computer and use it in GitHub Desktop.
const batchSize = 100000;
let totalDeleted = 0;
const cutoff = new Date("2024-01-01T00:00:00Z");
const col_name = "giscemisc_action";
while (true) {
const docs = db[col_name].find(
{ create_date: { $lt: cutoff } },
{ _id: 1 }
).limit(batchSize).toArray();
if (docs.length === 0) {
print("Deletion completed.");
break;
}
const ids = docs.map(doc => doc._id);
const res = db[col_name].deleteMany({ _id: { $in: ids } });
totalDeleted += res.deletedCount;
print("Deleted in this batch: " + res.deletedCount + " | Total deleted: " + totalDeleted);
sleep(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment