繁体   English   中英

根据Mongodb中集合中的dob更新年龄

[英]Update age based on dob in a collection in Mongodb

我的一个收藏夹中有1000个文档。

{“ _id”:ObjectId(“ 56d97671f6ad671b7d1c3d76”),“ parseId”:“ TdKxj9FFPY”,“电话”:“ 6643545645”,“ dob”:“ 15-06-87”,“年龄”:121“ createdAt”:ISODate (“ 2016-03-01T16:39:00.947Z”),“ updatedAt”:ISODate(“ 2016-03-01T16:39:00.947Z”),“ __v”:0}

{“ _id”:ObjectId(“ 56d97671f6ad671b7d1c3d76”),“ parseId”:“ TdKxj9FFPY”,“电话”:“ 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”,“ age”:152“ createdAt”:ISODate (“ 2016-03-01T16:39:00.947Z”),“ updatedAt”:ISODate(“ 2016-03-01T16:39:00.947Z”),“ __v”:0}

...................

...................

但是,一些年龄值都DOB的wrong.The数值right.So我需要更新的时代基于手动单个查询DOB值是多少?

最终我找到了解决方案,我只是将集合导出到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>

编辑:我对第一个不好。

将您的数据导出到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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM