簡體   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