0

我有以下示例数据,

[
    {
        "_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"
    }
]

我需要访问此数据中的每个数组,其中每个数组都有子字段。我需要在 CSV 文件中显示子字段。我的数据库中有大量数据。我需要将其导出到 CSV 文件。我使用 json2csv 进行 CSV 文件转换。但我无法访问数组字段值,除非我提到数组索引。我需要访问每个字段和数组字段而不提及数组索引。

我的代码:

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');
                });

请有人帮我解决这个问题,我为此苦苦挣扎了很长时间。提前致谢。 我在这里包含了我的 CSV 文件输出。.请调查一下并给我一些解决方案

4

1 回答 1

0

我不确定json2csv unwind在这种情况下到底做什么,但你可以自己在管道中使用它:

db.collection.aggregate([
    {
        "$match": {
            createdDate: { "$gte": start, "$lte": end }, 
            defaultAccountId: userParam.defaultAccountId,
            status: 'Completed' 
        }
    },
    {
        "$unwind": "$operationDisplay"
    }
]);
于 2020-04-05T06:25:03.037 回答