我在其中一个系列中有1000个文件。
{" _id":ObjectId(" 56d97671f6ad671b7d1c3d76")," parseId": " TdKxj9FFPY","电话":" 6643545645"," dob":" 15-06-87",& #34;年龄":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","年龄":100 " createdAt&#34 ;: 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&#34 ;: ISODate(" 2016-03-01T16:39:00.947Z")," updatedAt": ISODate(" 2016-03-01T16:39:00.947Z")," __ v":0}
...................
...................
但是年龄的某些值是错误的。 dob 的值是正确的。所以我需要在单个基础上更新基于dob的年龄值手动查询?
答案 0 :(得分:1)
最后我找到了一个解决方案。我只是将集合导出到json文件中,并使用js函数更新所有文档,并将集合导入数据库。
<强> 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 :(得分:0)
编辑:我对第一个不好。
将您的数据导出为JSON并通过它运行并重新导入它。你唯一的依赖是fs和momentjs。
$ch variable
年龄输出= [29,23,74];