我有以下代码
const fs = require('fs');
const { Transform } = require('json2csv');
const { UserModel } = require('./models/user.model');
const users = UserModel.aggregate([
{
$sort: {
_id: -1,
},
},
{
$limit: 100,
},
{
$project: {
_id: 0,
fullName: 1,
mobile: 1,
},
},
]).cursor().exec();
const fields = ['fullName', 'mobile'];
const opts = { fields };
const transformOpts = { highWaterMark: 16384, encoding: 'utf-8' };
// const input = fs.createReadStream('./public/users.json', { encoding: 'utf8' });
const output = fs.createWriteStream('./public/data.csv', { encoding: 'utf8' });
const json2csv = new Transform(opts, transformOpts);
const processor = users.pipe(json2csv).pipe(output);
如果执行此查询,则data.csv文件仅包含
"fullName","mobile"
,但如果来自静态json文件的管道,即user.json
,则data.csv
包含所有行。我做错了什么?有什么主意吗?