首页 文章

在Mongodb的集合中基于dob更新年龄

提问于
浏览
0

我在其中一个集合中有1000个文档 .

{“_ id”:ObjectId(“56d97671f6ad671b7d1c3d76”),“parseId”:“TdKxj9FFPY”,“phone”:“6643545645”,“dob”:“15-06-87”,“age”:121“createdAt”: ISODate(“2016-03-01T16:39:00.947Z”),“updatedAt”:ISODate(“2016-03-01T16:39:00.947Z”),“__ v”:0} {“_ id”:ObjectId(“ 56d97671f6ad671b7d1c3d76“),”parseId“:”TdKxj9FFPY“,”phone“:”9847523654“,”dob“:”15-06-93“,”age“:100”createdAt“:ISODate(”2016-03-01T16: 39:00.947Z“),”updatedAt“:ISODate(”2016-03-01T16:39:00.947Z“),”__ v“:0} {”_ id“:ObjectId(”56d97671f6ad671b7d1c3d76“),”parseId“:” TdKxj9FFPY“,”电话“:”4564646646“,”dob“:”15-06-43“,”年龄“:152”createdAt“:ISODate(”2016-03-01T16:39:00.947Z“),”updatedAt “:ISODate(”2016-03-01T16:39:00.947Z“),”__ v“:0} ................... ....... ............

但是 age 的一些值是错误的 . dob 的值是正确的 . 所以我需要手动更新基于单个查询中的dob的年龄值?

2 回答

  • 0

    最后我找到了一个解决方案 . 我只是将集合导入到json文件中,并使用js函数更新所有文档,并将集合导入到db中 .

    html

    <html>
    
    <head>
        <script type="text/javascript" src="https://rawgit.com/eligrey/FileSaver.js/master/FileSaver.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    
    <body>
        <button id="save">save</button>
    </body>
    <script type="text/javascript">
    function getAge(dateString) {
        var today = new Date();
        var birthDate = new Date(dateString);
        var age = today.getFullYear() - birthDate.getFullYear();
        var m = today.getMonth() - birthDate.getMonth();
        if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
            age--;
        }
        return age;
    }
    var exportData;
    $.getJSON("input.json", function(data) {
        exportData = data;
        exportData.forEach(function(item, index) {
            // console.log('item dob', item.dob);
            var dobInfo = item.dob.split('-');
            var dd = dobInfo[0];
            if (dd < 10) {
                dd = '0' + dd;
            }
            var mm = dobInfo[1];
            if (mm < 10) {
                mm = '0' + mm;
            }
            var yy = dobInfo[2];
            yy = (yy < 17) ? '20' + yy : '19' + yy;
            // console.log('dd', dd);
            // console.log('mm', mm);
            // console.log('yy', yy);
            var newdate = mm + '-' + dd + '-' + yy;
            // console.log('newdate',newdate);
            console.log('index[' + index + ']', item.dob);
            var age = getAge(newdate);
            console.log('age--->', age);
            exportData[index].age = age;
        });
    });
    document.getElementById('save').onclick = function() {
        var textToSave = JSON.stringify(exportData),
            filename = 'output.json',
            blob = new Blob([textToSave], {
                type: "text/plain;charset=utf-8"
            });
    
        saveAs(blob, filename);
    }
    </script>
    
    </html>
    
  • 1

    编辑:我对第一个不好 .

    将您的数据导出为JSON并通过它运行并重新导入它 . 你唯一的依赖是fs和momentjs .

    var moment = require('moment'),
        fs = require('fs'),
        json = [
            { "_id":"56d97671f6ad671b7d1c3d76", "parseId": "TdKxj9FFPY", "phone": "6643545645", "dob": "15-06-87", "age": 121, "createdAt": "2016-03-01T16:39:00.947Z", "updatedAt": "2016-03-01T16:39:00.947Z", "__v": 0 },
            { "_id":"56d97671f6ad671b7d1c3d76", "parseId": "TdKxj9FFPY", "phone": "9847523654", "dob": "15-06-93", "age": 100, "createdAt": "2016-03-01T16:39:00.947Z", "updatedAt": "2016-03-01T16:39:00.947Z", "__v": 0 },
            { "_id":"56d97671f6ad671b7d1c3d76", "parseId": "TdKxj9FFPY", "phone": "4564646646", "dob": "15-06-43", "age": 152, "createdAt": "2016-03-01T16:39:00.947Z", "updatedAt": "2016-03-01T16:39:00.947Z", "__v": 0 }
        ];
    
    Object.keys(json).forEach(function(key) {
        age = moment().diff(moment(json[key].dob,"DD/MM/YY"),'years');
    
        //
        //dates before 1970 are negative
        //
        if (parseInt(age) < 0) {
            age += 100;
        }
    
        json[key].age = age;
    });
    
    fs.writeFile('data.json', JSON.stringify(json), function (err) {
      if (err) return console.log(err);
      console.log('compeleted');
    });
    

    年龄产出= [29,23,74];

相关问题