var saveDay=30;//保留30天数据
var months = ['01','02','03','04','05','06','07','08','09','10','11','12'];
var days = ['01','02','03','04','05','06','07','08','09'];//解决缺0的问题
var i;//删除前30-前40天的日表
for (i = 10; i > 0; i--) {
var currentTimeMill=(new Date()).valueOf();//当天值转换成毫秒值
//print("currentTimeMill:"+currentTimeMill);
var deleteTimeMill=currentTimeMill - (30 + i) * 24 * 60 * 60 * 1000
//print("deleteTimeMill:"+deleteTimeMill);
var dt=(new Date(deleteTimeMill));
var dyear=dt.getFullYear();
var dmonth=months[dt.getMonth()];
var dday=dt.getDate();
if (dday < 10) {
dday=days[dday-1];
}
//print("dt:"+dyear+dmonth+dday);
var deleteTable="record_"+dyear+dmonth+dday;//表名xxx_日期
print("deleteTable:"+deleteTable);
db.getCollection(deleteTable).drop();
}
mongo 127.0.0.1:27017/record_db /root/clearMongo/clearMongo.js >> /root/clearMongo/cm.json
解决问题:linux的mongo库磁盘占用95%,需要写js脚本定时清除1个月以前的数据