harry-potter
harry-potter

Reputation: 2049

Export an array in csv from MongoDB

I'm exporting documents of collection called team in MongoDB as csv file. For example I have this MongoDB document:

{
    "_id" : "149",
    "official_name" : "yaChAaoiqn",
    "common_name" : "VMlEXjTOZE",
    "country" : "YSpbYyPWSo",
    "championship" : "rwSbupqsln",
    "uniform" : [ 
        "first_colour", 
        "second_colour", 
        "third_colour"
    ]
}

I use the following command to export documents in csv:

Z:\Program Files\MongoDB\Server\3.0\bin>mongoexport --db paul --collection team
--type=csv --fields _id,official_name,common_name,country,championship,"uniform[0]","uniform[1]","uniform[2]" --out "C:\path\to\outputFile\output.csv"

This is the csv I have in output:

_id,official_name,common_name,country,championship,uniform[0],uniform[1],uniform[2]
149,yaChAaoiqn,VMlEXjTOZE,YSpbYyPWSo,rwSbupqsln,"","",""

The csv file tells me there are some problems with uniform.
How must I set the uniform field in the command to export from MongoDB rightly an array?

Upvotes: 2

Views: 3138

Answers (1)

robjwilkins
robjwilkins

Reputation: 5652

I dont think this is currently possible. I think your best bet is to use the uniform field without referring to the element index. All values from the array will then be written to the csv file, and you could correctly reformat them post-export.

mongoexport --db paul --collection team --type=csv --fields _id,official_name,common_name,country,championship,uniform --out "C:\path\to\outputFile\output.csv"

Upvotes: 2

Related Questions