I have the following sample data,
[
{
"_id": "5e88624da92cf342ac9357a3",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.705Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
}
],
"orderHistory": [
{
"Date": "2020-04-04T10:32:45.708Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:32:45.708Z",
"id": "9f6fb3d0-765f-11ea-9ec4-594499e83360",
"AI": 76,
"__v": 0,
"no": "76 - 20/21"
},
{
"_id": "5e88629d31683d4f3ccc95e8",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.094Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.094Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.095Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.095Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
}
],
"orderHistory": [
{
"Date": "2020-04-04T10:34:05.096Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:34:05.097Z",
"id": "cec180a0-765f-11ea-95c5-27ad067619f6",
"AI": 77,
"__v": 0,
"no": "77 - 20/21"
},
{
"_id": "5e88630031683d4f3ccc95e9",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:35:44.256Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:35:44.256Z",
"id": "09dbf710-7660-11ea-95c5-27ad067619f6",
"AI": 78,
"__v": 1,
"no": "78 - 20/21"
},
{
"_id": "5e8863910eb0363ad46e9fc6",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:38:09.114Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:38:09.114Z",
"id": "60338ab0-7660-11ea-b15f-615914b43a17",
"AI": 79,
"__v": 1,
"no": "79 - 20/21"
},
{
"_id": "5e8863b90eb0363ad46e9fc7",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:38:49.166Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:38:49.167Z",
"id": "7812fdf0-7660-11ea-b15f-615914b43a17",
"AI": 80,
"__v": 1,
"no": "80 - 20/21"
}
]
I need to access every array in this data, in which each array has the subfields. I need to display the subfields in the CSV file. I have a large number of data's in my database. I need to export it to the CSV file.I used json2csv for CSV file conversion.But I can't access the array field values unless I mention the array Index. I need to access every field and array field without mentioning array index.
My code:
const { Parser, transforms: { unwind, flatten } } = require('json2csv');
const fs = require('fs');
var userss = await despatch.find({ createdDate: { "$gte": start, "$lte": end }, defaultAccountId: userParam.defaultAccountId, status: 'Completed' });
// const fields = ['operationDisplay.processName'];
const fields = [
{ value: "id", label: "id" },
{ value: "defaultAccountId", label: "DefaultAccountId" },
{ value: "color", label: "Color" },
{ value: "date", label: "Date" },
{ value: "grnNo", label: "GRN_No" },
{ value: "party", label: "Party" },
{ value: "buyer", label: "Buyer" },
{ value: "lotNo", label: "LotNo" },
{ value: "orderNo", label: "OrderNo" },
{ value: "manualDcNo", label: "ManualDcNo" },
{ value: "dyeingDcNo", label: "DyeingDcNo" },
{ value: "dyeingDcDate", label: "DyeingDcDate" },
{ value: "dyeingName", label: "dyeingName" },
{ value: "partyDcNo", label: "PartyDcNo" },
{ value: "partyDcRolls", label: "PartyDcRolls" },
{ value: "weight", label: "Weight" },
{ value: "vehicleNo", label: "VehicleNo" },
{ value: "driverName", label: "DriverName" },
{ value: "deliveryReference", label: "DeliveryReference" },
{ value: "deliverTo", label: "deliverTo" },
{ value: "reference", label: "Reference" },
{ value: "referName", label: "ReferName" },
{ value: "weight", label: "Weight" },
{ value: "fabricArray.color", label: "fabricArray_Color" },
{ value: "operationDisplay", label: "ProcessName" },
{ value: "operationDisplay.processName", label: "ProcessNames" },
]
unwind({ paths: ['operationDisplay', 'operationDisplay.processName'] });
flatten({ objects: false, arrays: true });
const json2csvParser = new Parser({ fields });
const csv = json2csvParser.parse(userss);
fs.writeFile(filePath, csv, function(err) {
if (err) throw err;
console.log('file saved');
});
Please, someone, help me with this, I am struggling with this for a long time. Thanks in advance. I have included my CSV file output here..Please look into it and provide me some solution
I'm not sure exactly what json2csv
unwind
do in this context but you can just use it in a pipeline yourself:
db.collection.aggregate([
{
"$match": {
createdDate: { "$gte": start, "$lte": end },
defaultAccountId: userParam.defaultAccountId,
status: 'Completed'
}
},
{
"$unwind": "$operationDisplay"
}
]);