我已经尝试过使用名为 json2csv 的 npm 包。对于最多 75 000 条记录,它工作正常。当数据超过该数据时,我没有从回调函数 exporttocsv 得到任何响应,如下所示。
const json2csv = require('json2csv').parse;
var today = new Date();
var mongoClient = require('mongodb').MongoClient
, assert = require('assert');
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = dd + '_' + mm + '_' + yyyy;
router.put('/mass_report', (req, res) => {
mass_data_download();
res.json("Mass report download initiated");
});
function exporttocsv(data,name, callback) {
/* Start: Json to xlsx conversion */
if (!fs.existsSync('./csv/'+today+'/')) {
fs.mkdirSync('./csv/'+today+'/');
}
var csv = json2csv(data);
var fname = './csv/'+today+'/' +name+ new Date().getTime() + '.csv';
fs.writeFileSync(fname, csv, 'binary',(error,response)=>{
console.log(error);
console.log(response);
});
callback(fname);
}
function mass_data_download(){
db.collection('mass_data').aggregate([
{$match:{
created_on: {
$gte: new Date("2017-09-01T00:00:00.000Z"),
}
}}
]).sort({_id:-1}).toArray( function (error, response) {
if(error){
console.log(error)
}
else{
console.log(response.length);
exporttocsv(response,'mass_report', function (fname) {
console.log('reports download complted');
})
}
})
}
将数据导出到 csv 时是否有任何限制?或者如何通过任何其他替代方案来实现这一目标?