Mongodb中批量替换(replace)

2022/1/25 19:05:51

本文主要是介绍Mongodb中批量替换(replace),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  原因:整个项目迁移之后,之前上传的文件路径url在数据库中都是以ip为前缀的,例如(http://111.111.111.111:8089/u/0/0/202112/o/8a623b0fa09b4ccf8f1e0ee57ee43f3c.jpg),现在都不能访问了,所以要进行替换成域名。吃一堑长一智啊兄弟们,前缀一定要用域名,迁移后直接重新解析域名就行了!!!

废话不多说,直接看sql:

 db.flow_data.find(
    {
        'formDatas':{"$elemMatch": {"value": /^.*11.192.81.91.*$/}}
    }
).forEach(
    function (item) {
        let runFlag = 0
        let arr = item.formDatas;
        for(const index in arr){
            let flag = arr[index].value.indexOf('11.192.81.91');
            if(flag != -1){
             let str1 = arr[index].value
                let a = str1.replace(/11.192.81.91/g,'crm.aaaaaa.cn')//
                arr[index].value = a
                runFlag = 1
            }
        }
        print(JSON.stringify(arr))
        if(runFlag){
            let flowNo = item.flowNo
            print(flowNo)
            item.formDatas = arr
            db.getCollection("flow_data").update( { 'flowNo':flowNo }, { $set: { 'formDatas':arr } } )
        }
        print("-------------------------------------------------")
    }
 )

 

下面是库中数据格式:

{
    "_id": ObjectId("61cef66857ee926adab7c27c"),
    "flowNo": "1000331640953448",
    "isContract": NumberInt("0"),
    "flowTmpId": ObjectId("618904b87b2d3446d0812c6a"),
    "initUserId": NumberInt("100033"),
    "initTime": NumberLong("1640953448"),"flowTmpName": "费用报销",
    "flowTitle": "xxx的费用报销申请",
    "formDatas": [
        {
            "vModel": "field10",
            "value": "[{\"key\":\"1000331639107965\",\"value\":\"xxx的备用金申请\"}]",
            "dataType": "relevance",
            "label": "关联备用金审批单"
        },
        {
            "vModel": "field11",
            "value": "超出金额,不用报销",
            "dataType": "textarea",
            "label": "备注"
        },
        {
            "vModel": "field12",
            "value": "http://11.192.81.91:8089/u/0/0/202112/o/8a623b0fa09b4ccf8f1e0ee57ee43f3c.jpg,http://11.192.81.91:8089/u/0/0/202112/o/582472c5b2ed42d6b1886bfc055d8352.jpg,http://11.192.81.91:8089/u/0/0/202112/o/7e57d8cc57e84438974225f573ed21be.jpg",
            "dataType": "upload-img",
            "label": "图片"
        },
        {
            "vModel": "field13",
            "value": "[{\"fileName\":\"12月活动报销.pdf\",\"filePath\":\"http://11.192.81.91:8089/u/0/0/202112/aceddd8413884b57bb54dccdeba5dc72.pdf\",\"fileId\":\"c9cdc39cf4dd4b9081b71792c532e777\",\"fileExt\":\".pdf\"},{\"fileName\":\"部门聚餐.pdf\",\"filePath\":\"http://11.192.81.91:8089/u/0/0/202112/3845199de6144846b45e368ab972d6ca.pdf\",\"fileId\":\"a324cff69dc64fef8fddbe9ccaf0a197\",\"fileExt\":\".pdf\"},{\"fileName\":\"零食.pdf\",\"filePath\":\"http://11.192.81.91:8089/u/0/0/202112/e2a6efa5651f4ccd94242df67326f14f.pdf\",\"fileId\":\"784d6108777146f08a59b49752497608\",\"fileExt\":\".pdf\"},{\"fileName\":\"12月活动经费.xls\",\"filePath\":\"http://11.192.81.91:8089/u/0/0/202112/dc67f27c27e64ddda22d37522b3e8768.xls\",\"fileId\":\"ddad34fe6fef461ca232fe73fabb82a4\",\"fileExt\":\".xls\"}]",
            "dataType": "upload-text",
            "label": "附件"
        }
    ],
    "nodes": [
        {
            "nodeName": "审批人",
            "nodeType": NumberInt("1"),
            "result": NumberInt("1"),
            "counterSign": NumberInt("1"),
            "appMembers": [
                {
                    "userId": NumberInt("100150"),
                    "userName": "高竹君",
                    "isAffirm": NumberInt("1"),
                    "reason": "",
                    "nodeOrd": NumberInt("1"),
                    "appOrd": NumberInt("1"),
                    "affirmTime": NumberLong("1640953550"),
                    "duration": 0.03
                }
            ],
            "nodeOrd": NumberInt("1"),
            "current": NumberInt("0")
        },
        {
            "nodeName": "审批人",
            "nodeType": NumberInt("1"),
            "result": NumberInt("1"),
            "counterSign": NumberInt("1"),
            "appMembers": [
                {
                    "userId": NumberInt("100025"),
                    "userName": "李文心",
                    "isAffirm": NumberInt("1"),
                    "reason": "",
                    "nodeOrd": NumberInt("2"),
                    "appOrd": NumberInt("1"),
                    "affirmTime": NumberLong("1640954949"),
                    "duration": 0.39
                }
            ],
            "nodeOrd": NumberInt("2"),
            "current": NumberInt("0")
        },
        {
            "nodeName": "审批人",
            "nodeType": NumberInt("1"),
            "result": NumberInt("1"),
            "counterSign": NumberInt("0"),
            "appMembers": [
                {
                    "userId": NumberInt("100058"),
                    "userName": "张荣华",
                    "isAffirm": NumberInt("0"),
                    "nodeOrd": NumberInt("3"),
                    "appOrd": NumberInt("0"),
                    "affirmTime": NumberLong("0")
                },
                {
                    "userId": NumberInt("100178"),
                    "userName": "梁海霞",
                    "isAffirm": NumberInt("1"),
                    "reason": "",
                    "nodeOrd": NumberInt("3"),
                    "appOrd": NumberInt("1"),
                    "affirmTime": NumberLong("1640955017"),
                    "duration": 0.02
                }
            ],
            "nodeOrd": NumberInt("3"),
            "current": NumberInt("0")
        },
        {
            "nodeName": "审批人",
            "nodeType": NumberInt("1"),
            "result": NumberInt("0"),
            "counterSign": NumberInt("1"),
            "appMembers": [
                {
                    "userId": NumberInt("100178"),
                    "userName": "梁海霞",
                    "isAffirm": NumberInt("0"),
                    "nodeOrd": NumberInt("4"),
                    "appOrd": NumberInt("0"),
                    "affirmTime": NumberLong("0")
                },
                {
                    "userId": NumberInt("100217"),
                    "userName": "林蓥蓥",
                    "isAffirm": NumberInt("0"),
                    "nodeOrd": NumberInt("4"),
                    "appOrd": NumberInt("0"),
                    "affirmTime": NumberLong("0")
                }
            ],
            "nodeOrd": NumberInt("4"),
            "current": NumberInt("0")
        },
        {
            "nodeName": "抄送人",
            "nodeType": NumberInt("2"),
            "result": NumberInt("0"),
            "counterSign": NumberInt("1"),
            "appMembers": [
                {
                    "userId": NumberInt("100025"),
                    "userName": "李文心",
                    "isAffirm": NumberInt("0"),
                    "nodeOrd": NumberInt("5"),
                    "appOrd": NumberInt("0"),
                    "affirmTime": NumberLong("0")
                },
                {
                    "userId": NumberInt("100058"),
                    "userName": "张荣华",
                    "isAffirm": NumberInt("0"),
                    "nodeOrd": NumberInt("5"),
                    "appOrd": NumberInt("0"),
                    "affirmTime": NumberLong("0")
                }
            ],
            "nodeOrd": NumberInt("5")
        }
    ],
    "step": NumberInt("4"),
    "createTime": NumberLong("1640953448"),
    "createUserId": NumberInt("100033"),
    "endResult": NumberInt("2"),
    "companyId": ObjectId("6165173076586f41d30a6267"),
    "deptId": ObjectId("6188bd4e7b2d3446d081251d"),
    "appTime": NumberLong("1640955017"),
    "isDelete": NumberInt("0"),
    "device": "pc",
    "revocTime": NumberLong("1640958127")
}

 



这篇关于Mongodb中批量替换(replace)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程