美文网首页
批量修改mongodb字段.md

批量修改mongodb字段.md

作者: 平凡的运维之路 | 来源:发表于2023-12-27 11:42 被阅读0次

脚本说明

  • 该脚本用于批量修改mongodb字段,根据时间范围循环执行.

  • 增加工作时间范围判断和mongodb 认证判断

  • 增加日志输出

  • 脚本代码

#!/bin/bash
MongoHostAndPort="127.0.0.1:27017"
# MongoBin="/home/mongodb/mongodb_3.2.12/bin/mongo"
MongoBin="/root/mongodb/mongo"
logfile="./log/remove_detail.log"
start_date="2022-01-01"
end_date="2022-12-31"
mongodb_user=ccod
mongodb_pwd=TKBXqnsoft2019
mopngodb_auth=true

#日志输出
logmod(){
        echo "`date +%Y-%m-%d" "%H:%M:%S` "$*"" >> $logfile     
}

#检查脚本执行工作时间
function check_for_hour() {
  function check_hour() {
    current_hour=$(date +%H)
    if [ $current_hour -ge 8 ] && [ $current_hour -le 22 ]; then
      logmod  INFO  $current_hour  not In job Time 08 hour ==== 22 hour  sleep 3600s
      sleep 3600
    else
      logmod  INFO  $current_hour   In job Time 08 hour ==== 22 hour Break For
      break
    fi
  }
  while true; do
    check_hour
  done
}

#自动获取需要执行企业id
function get_ent_id_list(){
    if [ "$mopngodb_auth" == true ];then
        ent_id_list=$(echo -e "show dbs"|$MongoBin  $MongoHostAndPort|awk '{print $1}'|egrep '^[0-9]')
    else
        ent_id_list=$(echo -e "show dbs"|$MongoBin  $MongoHostAndPort --authenticationDatabase admin -u$mongodb_user -p$mongodb_pwd |awk '{print $1}'|egrep '^[0-9]' )
    fi
    echo  $ent_id_list
}

#删除执行函数
 for Entid in  $(get_ent_id_list)
do
    # 计算开始和结束时间戳
    start_timestamp=$(date -d "$start_date" +%s)
    end_timestamp=$(date -d "$end_date" +%s)

    # 输出开始和结束日期
    logmod  INFO  "remove test1集合中 子集 开始日期: $(date -d @$start_timestamp +%Y-%m-%d)"   "结束日期: $(date -d @$end_timestamp +%Y-%m-%d)"

    # 循环输出每一天的日期
    current_timestamp=$start_timestamp
    while [ $current_timestamp -le $end_timestamp ]; do
        start_time_timestamp=$((current_timestamp))
        current_timestamp=$((current_timestamp + 86400))  # 每天加86400秒
        end_time_timestamp=$current_timestamp
        new_start_time_timestamp=$start_time_timestamp"000"
        new_end_time_timestamp=$end_time_timestamp"000"
        # echo $new_start_time_timestamp,$new_end_time_timestamp
        logmod  INFO Run mongodb Command $Entid -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})" 

        if [ "$mopngodb_auth" == true ];then
            echo -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})" |$MongoBin  $MongoHostAndPort/$Entid --authenticationDatabase admin -u$mongodb_user -p$mongodb_pwd
        else
            echo -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})" |$MongoBin  $MongoHostAndPort/$Entid
        fi
        check_for_hour
        sleep 1
    done
done

使用命令行批量进行替换修改操作

  • 调整前结果
> db.fastdfs.find()
{ "_id" : ObjectId("65a9e4cb03febb56295304d4"),  "fastdfs_url" : "http://1.1.1.1:8099/g10/M00/39/D9/CmQA6WEJ5NGAEtsoAABqUGfXheo386.wav" }
  • 详细命令如下
db.ent_record_fastdfs_url.find({ $or: [{ "fastdfs_url": { $regex: /1.1.1.1/ } }, { "original_url": { $regex: /1.1.1.1/ } }] }).forEach(function(doc) {
   db.ent_record_fastdfs_url.update(
      { "_id": doc._id },
      {
         $set: {
            "fastdfs_url": doc.fastdfs_url.replace(/jsdx.ccod.com/g, "2.2.2.2"),
            "original_url": doc.original_url.replace(/jsdx.ccod.com/g, "2.2.2.2"),
            // Add more fields here if needed
         }
      }
   );
});
  • 调整后结果
> db.fastdfs.find()
{ "_id" : ObjectId("65a9e4cb03febb56295304d4"),  "fastdfs_url" : "http://2.2.2.2:8099/g10/M00/39/D9/CmQA6WEJ5NGAEtsoAABqUGfXheo386.wav" }

相关文章

网友评论

      本文标题:批量修改mongodb字段.md

      本文链接:https://www.haomeiwen.com/subject/fqmrndtx.html