mongoDB 小指令碼總結

靜以致遠√團團發表於2016-01-10
批次建下個月的表
#!/bin/bash

source /root/.bash_profile

date_today=`date  +%Y-%m-%d`
date_month=`date -d '1 month' +%Y%m`
create_tab_log="/opt/scripts/mongodb/create_tab/create_tab_month.log"

echo '----------------------------------------------------'>>$create_tab_log
echo 'delete day is: '$date_today >> $create_tab_log
echo '----------------------------------------------------'>>$create_tab_log
for i in {1..9}
do
/usr/local/mongodb/bin/mongo  127.0.0.1:27017<< EOF >> $create_tab_log
use pmsadapterlog-ly
db.createCollection('pms_log_${date_month}0${i}');
db.pms_log_${date_month}0${i}.ensureIndex({"createTime":1});
db.pms_log_${date_month}0${i}.ensureIndex({"updateTime":1});
db.pms_log_${date_month}0${i}.ensureIndex({"prop":1});
db.pms_log_${date_month}0${i}.ensureIndex({"propName":1});
db.pms_log_${date_month}0${i}.ensureIndex({"hostIp":1});
db.pms_log_${date_month}0${i}.ensureIndex({"msgType":1});
db.pms_log_${date_month}0${i}.ensureIndex({"hotelCode":1});
db.pms_log_${date_month}0${i}.ensureIndex({"adapterKey":1});
db.pms_log_${date_month}0${i}.ensureIndex({"isSuccess":1});
db.pms_log_${date_month}0${i}.ensureIndex({"asyncIsSuccess":1});
db.pms_log_${date_month}0${i}.ensureIndex({"operationUser":1});
db.pms_log_${date_month}0${i}.ensureIndex({"isSendFog":1});
use pmsadapterlog-dc
db.createCollection('pms_log_${date_month}0${i}');
db.pms_log_${date_month}0${i}.ensureIndex({"createTime":1});
db.pms_log_${date_month}0${i}.ensureIndex({"updateTime":1});
db.pms_log_${date_month}0${i}.ensureIndex({"prop":1});
db.pms_log_${date_month}0${i}.ensureIndex({"propName":1});
db.pms_log_${date_month}0${i}.ensureIndex({"hostIp":1});
db.pms_log_${date_month}0${i}.ensureIndex({"msgType":1});
db.pms_log_${date_month}0${i}.ensureIndex({"hotelCode":1});
db.pms_log_${date_month}0${i}.ensureIndex({"adapterKey":1});
db.pms_log_${date_month}0${i}.ensureIndex({"isSuccess":1});
db.pms_log_${date_month}0${i}.ensureIndex({"asyncIsSuccess":1});
db.pms_log_${date_month}0${i}.ensureIndex({"operationUser":1});
db.pms_log_${date_month}0${i}.ensureIndex({"isSendFog":1});
exit
EOF
done

for i in {10..31}
do
/usr/local/mongodb/bin/mongo  127.0.0.1:27017<< EOF >> $create_tab_log
use pmsadapterlog-ly
db.createCollection('pms_log_${date_month}${i}');
db.pms_log_${date_month}${i}.ensureIndex({"createTime":1});
db.pms_log_${date_month}${i}.ensureIndex({"updateTime":1});
db.pms_log_${date_month}${i}.ensureIndex({"prop":1});
db.pms_log_${date_month}${i}.ensureIndex({"propName":1});
db.pms_log_${date_month}${i}.ensureIndex({"hostIp":1});
db.pms_log_${date_month}${i}.ensureIndex({"msgType":1});
db.pms_log_${date_month}${i}.ensureIndex({"hotelCode":1});
db.pms_log_${date_month}${i}.ensureIndex({"adapterKey":1});
db.pms_log_${date_month}${i}.ensureIndex({"isSuccess":1});
db.pms_log_${date_month}${i}.ensureIndex({"asyncIsSuccess":1});
db.pms_log_${date_month}${i}.ensureIndex({"operationUser":1});
db.pms_log_${date_month}${i}.ensureIndex({"isSendFog":1});
use pmsadapterlog-dc
db.createCollection('pms_log_${date_month}${i}');
db.pms_log_${date_month}${i}.ensureIndex({"createTime":1});
db.pms_log_${date_month}${i}.ensureIndex({"updateTime":1});
db.pms_log_${date_month}${i}.ensureIndex({"prop":1});
db.pms_log_${date_month}${i}.ensureIndex({"propName":1});
db.pms_log_${date_month}${i}.ensureIndex({"hostIp":1});
db.pms_log_${date_month}${i}.ensureIndex({"msgType":1});
db.pms_log_${date_month}${i}.ensureIndex({"hotelCode":1});
db.pms_log_${date_month}${i}.ensureIndex({"adapterKey":1});
db.pms_log_${date_month}${i}.ensureIndex({"isSuccess":1});
db.pms_log_${date_month}${i}.ensureIndex({"asyncIsSuccess":1});
db.pms_log_${date_month}${i}.ensureIndex({"operationUser":1});
db.pms_log_${date_month}${i}.ensureIndex({"isSendFog":1});
exit
EOF
done


刪除一個月前的表

#!/bin/bash

source /root/.bash_profile

date_today=`date  +%Y-%m-%d`
date_month_ago=`date -d '1 month ago' +%Y%m%d`
drop_log="/opt/scripts/mongodb/create_tab/drop_tab_month.log"

echo '----------------------------------------------------'>>$drop_log
echo 'delete day is: '$date_today >> $drop_log
echo '----------------------------------------------------'>>$drop_log
/usr/local/mongodb/bin/mongo  127.0.0.1:27017<< EOF >> $drop_log
use pmsadapterlog-ly
db.pms_log_$date_month_ago.drop();
use pmsadapterlog-dc
db.pms_log_$date_month_ago.drop();
exit
EOF




按某個欄位刪除表裡一個月前的資料
#!/bin/bash

source /root/.bash_profile

rm_date_today=`date  +%Y-%m-%d`
rm_date_month=`date -d '1 month ago' +%Y,%m,%d`
delete_log="/opt/scripts/mongodb/mongo_delete.log"

echo '----------------------------------------------------'>>$delete_log
echo 'delete day is: '$rm_date_today >> $delete_log
echo '----------------------------------------------------'>>$delete_log
/usr/local/mongodb/bin/mongo  127.0.0.1:27017<< EOF >> $delete_log
use pmsadapterlog-ly
db.pms_log.remove({createTime: {\$lt: new Date("$rm_date_month")},msgType:'updateavail'})
use pmsadapterlog-dc
db.pms_log.remove({createTime: {\$lt: new Date("$rm_date_month")},msgType:'updateavail'})
exit
EOF





來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29320885/viewspace-1974427/,如需轉載,請註明出處,否則將追究法律責任。

相關文章