1 shell中for循环清理
每次去连接一下mongo, 进行数据的remove操作
echo "Begin ..." >> /root/time.log; echo $(date +%Y-%m-%d-%H:%M:%S) >>/root/time.log; for((i=1;i<=100000000;i++)); do objectid=`/usr/local/mongodb/bin/mongo 127.0.0.1:37017/test -uadmin -padmin --quiet --eval "printjson(db.tickets.findOne({"date":{\\$lt:ISODate(\"2013-01-10T01 :16:33.303Z\")}},{"_id":1}))"`; /usr/local/mongodb/bin/mongo 127.0.0.1:37017/test -uadmin -padmin --quiet --eval "db.tickets.remove($objectid)"; echo " done " $objectid " " >> /root/tickets.log done echo $(date +%Y-%m-%d-%H:%M:%S) >>/root/time.log; echo "End ..." >> /root/time.log; echo " " >> /root/time.log
2 mongo shell中 for循环 清理
只连接一次mongo, 在mongo shell中for循环清理
[root@mongo ~]# more t.sh echo "Begin ..." >> /root/t.log; echo $(date +%Y-%m-%d-%H:%M:%S) >>/root/t.log; /usr/local/mongodb/bin/mongo 127.0.0.1:37017/test -uadmin -padmin t.js; echo $(date +%Y-%m-%d-%H:%M:%S) >>/root/t.log; echo "End ..." >> /root/t.log; echo " " >> /root/t.log [root@mongo ~]# [root@mongo ~]# [root@mongo ~]# more t.js for( var i=1 ; i<=100000000; i++ ) { var objectid = db.tickets.findOne({date:{$lt:ISODate("2013-01-10T01:16:33.303Z")}},{_id:1}); db.tickets.remove(objectid); } [root@mongo ~]#
实际测试, 第一种安全保险的多,但是毕竟慢,清理100G的数据 大概要3天 ,第二种快捷的多(据说有风险,但是我测试没有碰到过),清理100G的数据,用了6个小时.