I want to convert the result from aggregate to a csv. In the code (or pseudocode) below the results of the aggregate are stored in variable cursor
I want to return a csv as a response ( res.send(csv)
). Do I have to use res.set('Content-Type', 'content-type: text/csv')
?
The code below is a mix of NodeJs and pseudocode. Function covert_to_csv
indicates my ignorance about the subject.
const express = require('express')
const app = express();
app.get('/', (req, res) => {
let agg=[ /* a query in MongoDB */]
MongoClient.connect(URL,(err, client) => {
res.set('Content-Type', 'content-type: text/csv');
let collection = client.db('db').collection('col')
let cursor = collection.aggregate(agg)
let csv_file = covert_to_csv(cursor)
csv_file.toArray((error, result) => {
res.send(result);
});
});
app.listen(port, () => console.log(`listening on port ${port}!`));
Edit:
I am getting this response from Postman in JSON :
[
{
"Source": "entso-e",
"Dataset": "ActualTotalLoad",
"AreaName": "Germany",
"AreaTypeCode": "CTY",
"MapCode": "DE",
"ResolutionCode": "PT15M",
"Year": "2018",
"Month": "1",
"Day": "2",
"DateTimeUTC": "2018-01-02 00:00:00.0000000",
"ActualTotalLoadValue": "41412.38",
"UpdateTimeUTC": "2018-01-02 13:16:19.0000000"
},
{
"Source": "entso-e",
"Dataset": "ActualTotalLoad",
"AreaName": "Germany",
"AreaTypeCode": "CTY",
"MapCode": "DE",
"ResolutionCode": "PT15M",
"Year": "2018",
"Month": "1",
"Day": "2",
"DateTimeUTC": "2018-01-02 00:45:00.0000000",
"ActualTotalLoadValue": "40785.17",
"UpdateTimeUTC": "2018-01-02 13:16:19.0000000"
},
..... (more documents)
]
from NodeJS: How to convert 'aggregation'(cursor) object to CSV and return a CSV response from a request using Express?
No comments:
Post a Comment