How do I mongoexport attributes from an array of objects to CSV?

前端 未结 3 1946
故里飘歌
故里飘歌 2021-02-15 13:46

I need to export values of objects in an array to CSV. Let\'s say my document is:

{
    name:\"test\",
    types:[
        {type:\"A\"},
        {type:\"B\"},
                


        
3条回答
  •  被撕碎了的回忆
    2021-02-15 14:18

    If your happy with Perl then the ARJsonLib.pm library in the following article, provides the majority of functionality you'll need, to create your own little toy. Note the version in the article is a stub from a toy I hacked together that does exactly what you want along with some other stuff, but as not a mongoDB article it's lacking one function you'll need, that finds the fields/keys in a mongoDB collection, and stores them in an array, but trivial to reconstruct, just write yourself something that pull's n documents from your collection, pushes them into an array and calls findKeysInJsonColl(). Anyway a couple of the functions will take a MongoDB cursor as parameter, and:

    convertToDojoGrid()
    convertToExcel()
    

    Again the CSV output is missing, but trivial to add back to convertToExcel().

    e.g.

    ...
    my $iRows  = convertToExcel("/tmp/test.xlsx", $oMongoData, "", \@aOutFields, "xlsx");
    ...
    

    Where: $oMongoData is a MongoDB Cursor reference and @aOutFields an array containing the fields/keys you wish to appear in the sheet,

提交回复
热议问题