我有一个非常庞大的300k - 400k文档数据库,因此我必须定期使用string
将number
类型的某些字段更新为parseInt
。我使用typeof
运算符为其中一个字段创建了一个视图,并查询了所有string
个键的视图并更新了这些文档。
以下是我的观点map
功能:
function (doc) {
if (typeof doc.xyz !== 'undefined') {
emit(typeof doc.xyz, null);
}
}
接下来是我更新文档的方式:
function updateProperties(skip,field) {
var options = {reduce: false, limit: 1500, include_docs: true, key: "string"};
if(skip)
options.skip = skip;
db.query('test_indexes/'+field+'_type',options)
.then(function(result){
console.log('> Currently at offset: '+ result.offset);
result = result.rows.map(function(one) {
return one.doc;
});
if(result.length) {
for(var i=0 ;i< result.length; i++) {
if(field in result[i] && result[i][field]) {
try {
result[i][field] = parseInt(result[i][field],10);
}
catch(err) {
console.log('Error converting '+field+' at page '+page+' field: '+result[i][field]+ err.stack);
}
}
}
page++;
(function(page,findResult){
db.bulkDocs(findResult).then(function (result) {
totalDone+= result.length;
console.log('Done uploading page '+page+' of '+field+' > '+ 'Total Records updated: '+ totalDone);
updateProperties(totalDone,field);
}).catch(function (err) {
delete err.data;
console.log('Error updating bulkdocs for '+field+': '+ JSON.stringify(err));
updateProperties(totalDone,field);
});
})(page,result);
}
else {
fieldsConverted++;
totalDone = 0;
page = 0;
if(fieldsConverted === convert.length) {
console.log('******************************* DONE WITH '+ convert[fieldsConverted-1]+'**********************');
console.log('All done, Yay!');
return true;
}
else {
console.log('******************************* DONE WITH '+ convert[fieldsConverted-1]+'**********************');
updateProperties(null, convert[fieldsConverted]);
}
}
}).catch(function (err) {
console.log('Error while bulk fetching'+ JSON.stringify(err));
});
}
运行时,它会给出以下输出:
> Currently at offset: 0
Done uploading page 1 of xyz >Total Records updated: 1500
> Currently at offset: 3000
^C
abc@TempMach:~/aire20$ node server/data/bulkUpdate.js
> Currently at offset: 3000
Done uploading page 1 of xyz >Total Records updated: 1500
> Currently at offset: 6000
Done uploading page 2 of xyz >Total Records updated: 3000
> Currently at offset: 9000
现在你可以看到,当我想跳过1500个文档时,我希望我的偏移量在更新后为1500而不是3000。最后,如果说我有10个文档作为一个整体,它只会更新其中的5个并说 全部完成 并退出。我错过了什么?