将mongoDb数据导出为ex​​cel文件格式

时间:2018-03-15 10:16:20

标签: node.js mongodb mongoexport

我们正在尝试以Excel工作表格式导出数据。我们的数据如下所示:

{
        "_id": "PR",
        "rating_group": [
            {
                "rating": "Excellent",
                "count": 3
            },
            {
                "rating": "Very Good",
                "count": 3
            },
            {
                "rating": "Good",
                "count": 1
            },
            {
                "rating": "Poor",
                "count": 1
            }
        ],
        "totalScore": 638,
        "totalCount": 8
    },{
        "_id": "RR",
        "rating_group": [
            {
                "rating": "Excellent",
                "count": 4
            },
            {
                "rating": "Very Good",
                "count": 6
            },
            {
                "rating": "Good",
                "count": 3
            },
            {
                "rating": "Poor",
                "count": 1
            }
        ],
        "totalScore": 780,
        "totalCount": 14
    }

我们正在使用mongo-xslx(link here)并尝试了一些手  我们以这种方式创建了一个模型,

       var data1 = [
                      {
                          name : result[i]._id,
                          ratings : [ result[i].rating_group
                          ],
                      totalScore : result[i].totalScore,
                      totalCount : result[i].totalCount,
                      }
                   ];

以及我们在excel文件中为每个单独的节点获取此数据(一个用于PR,另一个用于RR),如下所示,

name    ratings[0][0][rating]   ratings[0][0][count]    ratings[0][1][rating]   ratings[0][1][count]    ratings[0][2][rating]   ratings[0][2][count]    ratings[0][3][rating]   ratings[0][3][count]
RR  Excellent   3   Very Good   3   Good    1   Poor    1

但这并不是我们想要的。

我们希望我们的数据采用适当的excel格式,如下所示

name Excellent VeryGood Good Poor total_score  total_count
RR   3           3       1     1     638           8

我们只是坚持这一步。寻找解决方案或一些提示,以便我们可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要做的就是格式化JSON,然后使用mongo-xlsx插件将JSON数据转换为excel格式。以下代码段执行此操作。

let mongoXlsx = require('mongo-xlsx');
let fs = require('fs');

let data =
    [
        {
            "_id": "PR",
            "rating_group": [
                {
                    "rating": "Excellent",
                    "count": 3
                },
                {
                    "rating": "Very Good",
                    "count": 3
                },
                {
                    "rating": "Good",
                    "count": 1
                },
                {
                    "rating": "Poor",
                    "count": 1
                }
            ],
            "totalScore": 638,
            "totalCount": 8
        }, {
            "_id": "RR",
            "rating_group": [
                {
                    "rating": "Excellent",
                    "count": 4
                },
                {
                    "rating": "Very Good",
                    "count": 6
                },
                {
                    "rating": "Good",
                    "count": 3
                },
                {
                    "rating": "Poor",
                    "count": 1
                }
            ],
            "totalScore": 780,
            "totalCount": 14
        }
    ];



    //place for formatting data
data.map(x => {
    for (let grp of x.rating_group) {
        x[grp['rating']] = grp['count'];
    }
    let tempScore = x.totalScore;  //temporary storage made inorder to put total score and total count at last position
    let tempCount = x.totalCount;
    delete x.rating_group;   // deleting it as it is not necessary 
    delete x.totalCount;
    delete x.totalScore;
    x.totalScore = tempScore;  //pushing totalScore to second last position
    x.totalCount = tempCount;  //pushing totalCount to last position
});

let model = mongoXlsx.buildDynamicModel(data);
mongoXlsx.mongoData2Xlsx(data, model, function (err, res) {
    console.log('res');
    console.log(res);
})