unwind is not working as expected.
See original GitHub issuejson2csv version: ^4.5.2 node version: 10.16.0
const { Parser } = require('json2csv');
const fields = ['judgeName', 'source_product_id', 'target_products.product_id', 'target_products.score', 'time'];
const json2csvParser = new Parser({ fields, unwind: ['target_products'] });
const csvJudges = json2csvParser.parse(judges);
My data: judes:
[
{
"_id": "5d5a5170f9acfe362c61272c",
"judgeName": "hunnyjmjim",
"source_product_id": "323",
"target_products": [
{
"_id": "5d5a5170f9acfe362c61272e",
"product_id": "9096",
"score": 1
},
{
"_id": "5d5a5170f9acfe362c61272d",
"product_id": "48484",
"score": 2
}
],
"time": "2019-08-19T07:36:16.433Z",
"__v": 0
},
{
"_id": "5d5a83c91c9d440000e258df",
"judgeName": "hunnyjmjimq",
"source_product_id": "323",
"target_products": [
{
"_id": "5d5a5170f9acfe362c61272e",
"product_id": "9096",
"score": 1
},
{
"_id": "5d5a5170f9acfe362c61272d",
"product_id": "48484",
"score": 2
}
],
"time": "2019-08-12T07:36:16.433Z",
"__v": 0
},
{
"_id": "5d5b8a14c5c38f450ca39fb5",
"judgeName": "saitrinath",
"source_product_id": "448",
"target_products": [
{
"_id": "5d5b8a14c5c38f450ca39fba",
"product_id": "60015",
"score": 3
},
{
"_id": "5d5b8a14c5c38f450ca39fb9",
"product_id": "973",
"score": 2
},
{
"_id": "5d5b8a14c5c38f450ca39fb8",
"product_id": "81851",
"score": 1
},
{
"_id": "5d5b8a14c5c38f450ca39fb7",
"product_id": "70898",
"score": 3
},
{
"_id": "5d5b8a14c5c38f450ca39fb6",
"product_id": "274",
"score": 1
}
],
"time": "2019-08-20T05:50:12.613Z",
"__v": 0
}
]
I am getting output like this:
"judgeName","source_product_id","target_products.product_id","target_products.score","time"
,,"9096",1,
,,"48484",2,
,,"9096",1,
,,"48484",2,
,,"60015",3,
,,"973",2,
,,"81851",1,
,,"70898",3,
,,"274",1,
When I use unwind command, other fields(“judgeName”,“source_product_id”, “time”) are getting missing which is not expected.
Just check the issue.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10
Top Results From Across the Web
Unwind not working as expected · Issue #479 · zemirco/json2csv
juanjoDiaz It looks like Unwind is not working as expected in v 5.0.1 when using nested JSON data I have even looked at...
Read more >MongoDB unwind aggregation query giving unexpected results
The problem was that the network element format was inconsistent. ... the format across the collection, everything worked as expected.
Read more >I don't get unwind in aggregation - why do I need it? - Studio 3T
The default answer is that no document is generated at all, because there's nothing to work with. And that's why there's ...
Read more >Cannot unwind from first chance exceptions
The behavior you are seeing is expected, we do not unwind from first chance exceptions if Just My Code is disabled. There are...
Read more >Help re-grouping after unwind - Working with Data - MongoDB
I am implementing the Event Sourcing & CQRS patterns. I have a collection of documents with the events from users to manipulate models....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone else having this kind of problem, it has something to do with mongoose documents. I found a way to fix this by using
toObject()
at every item. So for your example i guess it’d be fixed by changing:const csvJudges = json2csvParser.parse(judges);
toconst csvJudges = json2csvParser.parse(judges.map(judge => judge.toObject()));
Hope this helps
I had the same problem since I was directly using the data returned by a mongoose query. This solution worked!!!