Several suggested answers to this question have missed the point about streams altogether.
This module can help https://www.npmjs.org/package/JSONStream
However, lets suppose the situation as described and write the code ourselves. You are reading from a MongoDB as a stream, with ObjectMode = true by default.
This will lead to issues if you try to directly stream to file - something like "Invalid non-string/buffer chunk" error.
The solution to this type of problem is very simple.
Just put another Transform in between the readable and writeable to adapt the Object readable to a String writeable appropriately.
Sample Code Solution:
var fs = require('fs'),
writeStream = fs.createWriteStream('./out' + process.pid, {flags: 'w', encoding: 'utf-8' }),
stream = require('stream'),
stringifier = new stream.Transform();
stringifier._writableState.objectMode = true;
stringifier._transform = function (data, encoding, done) {
this.push(JSON.stringify(data));
this.push('\n');
done();
}
rowFeedDao.getRowFeedsStream(merchantId, jobId)
.pipe(stringifier)
.pipe(writeStream).on('error', function (err) {
// handle error condition
}