Skip to content

Instantly share code, notes, and snippets.

@mehdimehdi
Last active December 31, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdimehdi/7953408 to your computer and use it in GitHub Desktop.
Save mehdimehdi/7953408 to your computer and use it in GitHub Desktop.
Cleaning up old documents in large collection
//getting the punchtab db to do the real db operations..
var punchtabDb = db.getSiblingDB('punchtab');
//the date to start from
var oldestDoc = punchtabDb.track.find({},{date_created:1}).sort({date_created : 1}).limit(1).toArray()[0];
var someOldDate = oldestDoc['date_created'];
var sleepAmount = 10 * 1000; //ten second
var failSafeDate = new Date(2013,05,13);
var going = punchtabDb.delete.findOne({});
var lock = [];
if (going) {
var go = going['go'];
var someTime = going['batchAmount'] || 1000 * 60 * 60 ;
var batchAmount = someTime; //the amount of data that will be deleted
} else {
var go = false;
}
//figuring out if we still need to run the job
while (go) {
//building the threshold date until when we need to delete, this is will keep going up
var newDate = new Date(someOldDate.getTime() + batchAmount);
if (newDate > failSafeDate) {
quit();
}
print("removing docs that are lte to "+ newDate);
//deleting the real data
punchtabDb.track.remove( { date_created : { $lte : newDate } } );
batchAmount = batchAmount + someTime; // increase the next threshold
sleep(sleepAmount);
going = punchtabDb.delete.findOne({});
go = going['go'];
//calculating the lock percentage based on result of db.serverStatus()
var status = db.serverStatus()['locks']['punchtab']['timeLockedMicros']['w'];
lock.push({lock:status,total:Date.now()});
if (lock.length > 10) {
lock.shift();
}
var lockTime = lock[lock.length-1]['lock'] - lock[0]['lock'];
var totalTime = (lock[lock.length-1]['total'] - lock[0]['total'])*1000;
var lockPercentage = lockTime/totalTime*100;
//formatting to 2 decimal places
print(parseFloat(Math.round(lockPercentage* 100) / 100).toFixed(2) + "%");
if (going['autoPilot']) {
print('going auto pilot!');
if (lockPercentage < 60.0) {
someTime = Math.round(someTime * 1.2);
print("increasing pace " + someTime);
} else if (lockPercentage > 80.0) {
someTime = Math.round(someTime * 0.8);
print("decreasing pace " + someTime);
} else {
someTime = someTime || 1000 * 60 * 60;
}
} else {
someTime = going['batchAmount'] || 1000 * 60 * 60;
}
}
print("done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment