zoukankan      html  css  js  c++  java
  • MongoDB 数据转为CSV文件

    //格式化时间
    Date.prototype.format = function(fmt) { 
         var o = { 
            "M+" : this.getMonth()+1,                 //月份 
            "d+" : this.getDate(),                    //
            "h+" : this.getHours(),                   //小时 
            "m+" : this.getMinutes(),                 //
            "s+" : this.getSeconds(),                 //
            "q+" : Math.floor((this.getMonth()+3)/3), //季度 
            "S"  : this.getMilliseconds()             //毫秒 
        }; 
        if(/(y+)/.test(fmt)) {
                fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
        }
         for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
             }
         }
        return fmt; 
    };

    var t={
    "0001":"饮料",
    "0002":"文具",
    "0003":"衣帽",
    "0004":"打车"
    };

    db.getCollection('collection_person').find({"companyId":"1234567890"}).skip(0).limit(2000).forEach(

       function(doc){
           var str = "";
           str += doc._id + ",";
           str += doc.personId + ",";
           str += doc.personName + ",";
           str += doc.employeeId + ",";
           str += doc.personAccount/100 + ",";
           if(doc.status == 1 || doc.status == 7) {
               str += doc.balance/100 + ",";
           } else {
               str += "0,";
           }
           str += doc.recoveryAmount/100 + ",";
           
           str += doc.createTime.format("yyyy-MM-dd hh:mm:ss") + ",";
           str += doc.updateTime.format("yyyy-MM-dd") + ",";       
           var ss = "";
           for(var i=0;i<doc.consumeTypeList.length;i++) {
              if(ss.length>0) { 
                ss+="、";
              }
              ss += t[doc.consumTypeList[i] + ""];
           }
           str += ss;
           
           print(str)
       }
    )

     从控制太copy出打印数据,放到空白文件中,后缀名改为xxx.csv。

    一切OK!!!!

  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/xingtangxiaoga/p/10876242.html
Copyright © 2011-2022 走看看