Last active
October 30, 2016 13:57
-
-
Save AvnerCohen/471b59c61c42c136935fc7878e3a1938 to your computer and use it in GitHub Desktop.
Mongo Query to Delete Celery succesfull entries on a large collection, slowly but surely.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var COUNTER = 900; | |
function deleteSome(count) { | |
var itemsToDel = db.tasks.find({status: "SUCCESS"}, {_id: 1}).sort({created_at: 1}).limit(count).toArray(); | |
var IDs = itemsToDel.map(function(item){ return item["_id"]}) | |
if (IDs.length > 0 ) { | |
db.tasks.remove({"_id": {"$in": IDs } }); | |
sleep(5000); | |
} else { | |
COUNTER++; | |
print("delete skipped - " + COUNTER); | |
} | |
} | |
//LOG BEFORE | |
var beforeCount = db.tasks.count(); | |
print(beforeCount) | |
//USAGE | |
for (; COUNTER > 0; COUNTER--) { deleteSome(2000)} | |
//COUNTER | |
var afterCount = db.tasks.count(); | |
print("Reduced" + (beforeCount - afterCount) + ", And still to go - " + afterCount); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment