mongodb中批量將時間戳轉變通用日期格式


1,官網提供的mongodb遍歷腳本:

官方文檔地址:https://docs.mongodb.org/manual/tutorial/remove-documents/
>var arr = ["ab","cd","ef"]
>var show = function(value,index,ar){ print(value) }
>arr.forEach(show)
ab
cd
ef

2。mongodb的模糊查詢

mongodb模糊查詢參考:http://blog.csdn.net/5iasp/article/details/20714943,須要找出全部時間戳的數據記錄,由於時間戳多是以數字開頭,近期幾年的都是14XXX的,所以正則表達式以14開頭搜索。實現方式例如以下:

mongos> use pos
switched to db pos
mongos> 
db.paymentinfo.find({"paymentTime": {$regex: '14', $options:'i'}}).count();
mongos> db.paymentinfo.find({"paymentTime": {$regex: '144', $options:'i'}}).count();
1995
mongos> 

查詢到有1995條記錄。蠻多的,須要處理記錄數不少。

建立遍歷函數:

db.cms_Content.find({"userId":"444333f107624489bae28140d1970bbc"}).forEach(function(x) {
    if(x.title&&x.fileName){
        print(x.contentId);
db.cms_Content.update({"contentId":x.contentId},{"$set":{"title":x.fileName}});
    }
})

PS:證明不能用,效果一般


3,首先刪除一部分txnType為1的脹數據

刪除查詢出來的集合數據

db.paymentinfo.remove( {"txnType": {$regex: '1', $options:'i'}}, 300 );

刪除所以查詢出來的記錄:

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

刪除paymentTime=0的數據記錄

db.paymentinfo.remove( { paymentTime : "0" } )

4,遺留問題須要解決

for (var i = 0, len = 3; i < len; i++) {var child=dschilds[i]; var id=child._id; printjson((id)); var paymentTime=child.paymentTime; printjson(paymentTime)}
批量改動校驗錯誤日期數據的期待解決的問題

 db.paymentinfo.update({"_id": ObjectId("55d56fdbe4b0c1f89b5356ae")},{$set:{"paymentTime" : "14400511608049527"}},true);
 var ds= db.paymentinfo.find({"paymentTime": {$regex: '144', $options:'i'}});
 for (var i = 0, len = 1; i < len; i++) {
    var child=dschilds[i]; 
    var id=child._id; 
    printjson(id);
    var paymentTime=child.paymentTime;  
    var datestr=paymentTime  
    #問題在這里,這個日期是時間戳,比方1440560826340的模式。請問下。在mongodb shell里面怎樣將時間戳變成字符串'2015-12-15 12:34:16'這種日期字符串呢?
     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"] = new Date(parseInt(paymentTime) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); printjson(a) });

看到這里也不能解決我的問題。所以思路停止了,toLocaleString()獲得的是GST的日期格式串,不是我須要的yyyy-mm-dd hh:mm:ss結構的日期格式數


5,找到突破口,使用javascript

mongodb官網已經報錯提示有說明,mongodb shell里面是能夠調用javascript腳本。這么說來,直接在窗體里面寫js腳本來實現就ok了,然后准備拿一條數據來驗證是否正確,結果成功了,驗證腳本例如以下:
– 單獨一條集合數據中。將時間戳變成日期字符串:

db.paymentinfo.find({"_id":ObjectId("55d56cbbe4b0c1f89b5356a4")}).forEach(function (a) {   
        #這個函數是在月、日、時分秒的個位數字前面補0操作的
        function tran_val(val){
            if(parseInt(val)<10){
                val="0" +val;
            }
            return val;
        }
        # 這里是paymentTime為時間戳
        var datenew = new Date(parseInt(paymentTime)); 

        # 獲取年月日
        var year=datenew.getFullYear(); 
        var month=tran_val(datenew.getMonth()+1);
        var date=tran_val(datenew.getDate()); 

        # 獲取時分秒
        var hour=tran_val(datenew.getHours());
        var minute=tran_val(datenew.getMinutes()); 
        var second=tran_val(datenew.getSeconds());  

        # 組裝成標准的日期格式yyyy-mm-dd hh:mm:ss 
        var datastr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
        a["paymentTime"]=datastr
        print(paymentTime); 

        printjson(a) }
    );

上面的樣例表明直接用js腳本能夠實現時間戳到日期格式轉變,那么以下就開始for循環批量改動:

  db.paymentinfo.update({"_id": ObjectId("55d56fdbe4b0c1f89b5356ae")},{$set:{"paymentTime" : "14400511608049527"}},true);
    # 使用遍歷數組的方式來操作144開頭的時間戳
     var ds= db.paymentinfo.find({"paymentTime": {$regex: '144', $options:'i'}});
     var dschilds=ds.toArray();
     for (var i = 0;i <dschilds.length ; i++) {
        var child=dschilds[i]; 
        var id=child._id; 
        var paymentTime=child.paymentTime;
        print(paymentTime);
        function tran_val(val){
                    if(parseInt(val)<10){
                        val="0" +val;
                    }
                    return val;
        }
        var datenew = new Date(parseInt(paymentTime)); 
        var year=datenew.getFullYear();     
        var month=tran_val(datenew.getMonth()+1);
        var date=tran_val(datenew.getDate()); 
        var hour=tran_val(datenew.getHours());
        var minute=tran_val(datenew.getMinutes()); 
        var second=tran_val(datenew.getSeconds());
        var datestr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
        # 這里開始進行改動操作
        db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :datestr}},true);
        db.paymentinfo.find({"_id": id});
     }
      # 使用遍歷數組的方式來操作145開頭的時間戳
     var ds= db.paymentinfo.find({"paymentTime": {$regex: '145', $options:'i'}});
     var dschilds=ds.toArray();
     for (var i = 0;i <dschilds.length ; i++) {
        var child=dschilds[i]; 
        var id=child._id; 
        var paymentTime=child.paymentTime;
        print(paymentTime);
        function tran_val(val){
                    if(parseInt(val)<10){
                        val="0" +val;
                    }
                    return val;
        }
        var datenew = new Date(parseInt(paymentTime)); 
        var year=datenew.getFullYear();     
        var month=tran_val(datenew.getMonth()+1);
        var date=tran_val(datenew.getDate()); 
        var hour=tran_val(datenew.getHours());
        var minute=tran_val(datenew.getMinutes()); 
        var second=tran_val(datenew.getSeconds());
        var datestr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
        db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :datestr}},true);
        db.paymentinfo.find({"_id": id});
     }   

6。碰到新的問題,統一日期格式,將斜杠變成橫杠

–批量改動日期 斜杠變成橫杠

     var ds= db.paymentinfo.find({"paymentTime": {$regex: '/', $options:'i'}});
     var dschilds=ds.toArray();
     for (var i = 0;i <dschilds.length; i++) {
        var child=dschilds[i]; 
        var id=child._id;
        var paymentTime=child.paymentTime;
        var paymentTime2=paymentTime.replace(/\//g,"-");
        db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :paymentTime2}},true);
        print(paymentTime);print(paymentTime2);
        db.paymentinfo.find({"_id": id});
     }   
-- insert data
insert into t1 select 1,'a' from db1.t2;
call db1.proc_get_fints

OK,到此圓滿解決,特別感謝給我啟示的網友 Aeolus@普 ,^_^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM