Mongodb batch change the time stamp common date format

1195 people read comment(1) Collection Report
Classification:

1, the official website provides mongodb traversal script:

Official document address: https://docs.mongodb.org/manual/tutorial/remove-documents/
Arr >var = "ab", "CD", "EF"
Show >var = function (value, index, AR) {print (value)}
>arr.forEach (show)
AB
CD
EF

2, mongodb fuzzy query

Mongodb fuzzy query reference:Http://prog3.com/sbdm/blog/5iasp/article/details/20714943, need to find all the time stamp of the data record, because the time stamp is in the beginning of the figure, the last few years are 14XXX, so the regular expression to the beginning of the 14 search, the way is as follows:

Mongos>Use POS
To DB switchedPOS
Mongos>
Db.paymentinfo.find ({"PaymentTime": {$regex:'14',$options:'i'}}) (.Count);
Db.paymentinfo.find mongos> ({"PaymentTime": {$regex:'144',$options:'i'}}) (.Count);
One thousand nine hundred and ninety-five
Mongos>

Query to have 1995 records, quite a number of records need to deal with a lot of.

Establish ergodic function:

DB.cms_Content.find({"UserId":"444333f107624489bae28140d1970bbc"}).forEach(function (X{)
(ifX.title& &X.fileName{)
(printX.contentId);
DB.cms_Content.update({"ContentId":X.contentId{}."$set": {"Title":X.fileName}});
}
})

PS: proof can not be used, the effect of general


3, the first part of the txnType to delete the expansion of the data for 1

Delete query from the collection of data

Db.paymentinfo.remove ({"TxnType": {$regex:'1',$options:'i'}},Three hundred);

Delete the record:

Db.paymentinfo.remove ({"TxnType": {$regex:'1',$options:'i'}});

Delete paymentTime=0 data record

DB.paymentinfo.remove({PaymentTime: "0" })

4, legacy issues need to be resolved

For (I var = 0, len = 3; I < len; i++) child=dschilds[i] {var; id=child._id VaR; printjson ((ID)); paymentTime=child.paymentTime printjson; VAR (paymentTime)}
Batch modify the expected date of the error data to solve the problem

Db.paymentinfo.update ({"_id"ObjectId ("55d56fdbe4b0c1f89b5356ae"{}).$set: {"PaymentTime":"14400511608049527"}},True);
 VarDb.paymentinfo.find ds= ({"PaymentTime": {$regex:'144',$options:'i'}});
 For(Var= IZeroLen =One; I < len; i++) {
    VarChild=dschilds[i];
    VarId=child._id;
Printjson (ID);
    VarPaymentTime=child.paymentTime;
    VarDatestr=paymentTime
    # here. This date is timestamp, such as 1440560826340 mode. Excuse me, inside the mongodb shell how will timestamp into a string'2015-12-15 12:34:16'such a date string?
Db.paymentinfo.update ({"_id"Id}, {$set: {"PaymentTime"Datestr}},True);
Db.paymentinfo.find ({"_id"Id});
}

Db.paymentinfo.find ({"_id"ObjectId ("55dd36dc45ce9e75b91eb340")}).ForEach(Function (a){A["PaymentTime"=]NewDate (parseInt) * (paymentTime) *One thousand(.ToLocaleString ().Replace () (/: \d{)One,Two}$/,''(printjson (a)});

See here also can not solve my problem, so the idea to stop, toLocaleString () get is GST date format string, not I need hh:mm:ss yyyy-mm-dd structure of date format


5,找到突破口,使用javascript

Mongodb's official website has reported error are illustrated, the mongodb shell is to call JavaScript script, so directly inside the window write JS script to achieve OK and be ready to take a data to validate the correctness, the successful, validation script are as follows:
- a single set of data, the time stamp into a date string:

Db.paymentinfo.find ({"_id"ObjectId ("55d56cbbe4b0c1f89b5356a4")}).ForEach(Function (a){   
        This function is # in the month and day, when a digital front and 0 operation
        Function Tran_val(VAL){
            If(parseInt (VAL) <Ten{)
Val="0"+val;
}
            ReturnVal;
}
        Here is the paymentTime for the timestamp #
        VarDatenew =NewDate (parseInt (paymentTime));

        # date to get
        VarYear=datenew.getFullYear ();
        VarMonth=tran_val (datenew.getMonth) +One);
        VaR日期= tran_val(datenew。getdate());

        #获取时分秒
        VaR小时= tran_val(datenew。gethours());
        VaR分钟= tran_val(datenew。getminutes());
        VaR二= tran_val(datenew。getseconds());

        #组装成标准的日期格式yyyy-mm-dd HH:毫米:SS
        VaRdatastr =年+“-”+月+“-”+日期+“”+小时+“:”+分钟+“:”+二;
[“付款时间”] = datastr
        打印(付款时间);

printjson(一)}
);

上面的例子表明直接用JS脚本可以实现时间戳到日期格式转变,那么下面就开始为循环批量修改:

分贝。paymentinfo。更新({“_id”objectID(:“55d56fdbe4b0c1f89b5356ae”)},{设置$:{“付款时间”“14400511608049527”},真正的);
    #使用遍历数组的方式来操作144开头的时间戳
     VaRDS =分贝。paymentinfo。找到({“付款时间”:{$正则表达式“144”$选项“我”});
     VaRdschilds = toarray() DS;
     对于VaR我=;我的<< dschilds.length;i++){
        VaR孩子= dschilds [我];
        VaR_id ID =孩子;
        VaR付款时间= child.paymenttime;
        打印(付款时间);
        功能 tran_val(Val){
                    如果(parseInt(Val)<){
val =“0”+瓦尔;
}
                    返回瓦尔;
}
        VaRdatenew =新的日期(parseInt(付款时间));
        VaR年datenew getfullyear() =;
        VaR月= tran_val(datenew getmonth() +。);
        VaR日期= tran_val(datenew。getdate());
        VaR小时= tran_val(datenew。gethours());
        VaR分钟= tran_val(datenew。getminutes());
        VaR二= tran_val(datenew。getseconds());
        VaR设定=年+“-”+月+“-”+日期+“”+小时+“:”+分钟+“:”+二;
        #这里开始进行修改操作
分贝。paymentinfo。更新({“_id”:标识,{设置$:{“付款时间”:设定} },真正的);
分贝。paymentinfo。找到({“_id”):标识;
}
      #使用遍历数组的方式来操作145开头的时间戳
     VaRDS =分贝。paymentinfo。找到({“付款时间”:{$正则表达式“145”$选项“我”});
     VaRdschilds = toarray() DS;
     对于VaR我=;我的<< dschilds.length;i++){
        VaR孩子= dschilds [我];
        VaR_id ID =孩子;
        VaR付款时间= child.paymenttime;
        打印(付款时间);
        功能 tran_val(Val){
                    如果(parseInt(Val)<){
val =“0”+瓦尔;
}
                    返回瓦尔;
}
        VaRdatenew =新的日期(parseInt(付款时间));
        VaR年datenew getfullyear() =;
        VaR月= tran_val(datenew getmonth() +。);
        VaR日期= tran_val(datenew。getdate());
        VaR小时= tran_val(datenew。gethours());
        VaR分钟= tran_val(datenew。getminutes());
        VaR二= tran_val(datenew。getseconds());
        VaR设定=年+“-”+月+“-”+日期+“”+小时+“:”+分钟+“:”+二;
分贝。paymentinfo。更新({“_id”:标识,{设置$:{“付款时间”:设定} },真正的);
分贝。paymentinfo。找到({“_id”):标识;
}

6、碰到新的问题,统一日期格式,将斜杠变成横杠

–批量修改日期斜杠变成横杠

     VaRDS =分贝。paymentinfo。找到({“付款时间”:{$正则表达式“/”$选项“我”});
     VaRdschilds = toarray() DS;
     对于VaR我=;我的<< dschilds.length;i++){
        VaR孩子= dschilds [我];
        VaR_id ID =孩子;
        VaR付款时间= child.paymenttime;
        VaRpaymenttime2 =付款时间。取代(/ \、“-”);
分贝。paymentinfo。更新({“_id”:标识,{设置$:{“付款时间”:paymenttime2 } },真正的);
        打印(付款时间);打印(paymenttime2);
分贝。paymentinfo。找到({“_id”):标识;
}

好吧,到此圆满解决,特别感谢给我启发的网友风神”普,^ _ ^

猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:99999 4次
    • 积分:一万六千零四十九
    • 等级:
    • 排名:319名第
    • 原创:438篇
    • 转载:3篇
    • 译文:2篇
    • 评论:523条
    我的课程
    博客专栏
    技术网址连接
    http://dev.mysql.com/doc/
    文章分类
    文章存档
    最新评论