从MongoDB导出csv中的数组 [英] Export an array in csv from MongoDB

查看:667
本文介绍了从MongoDB导出csv中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中导出名为team的集合文档作为csv文件。
例如,我有这个MongoDB文档:

  {
_id:149 b $ bofficial_name:yaChAaoiqn,
common_name:VMlEXjTOZE,
country:YSpbYyPWSo,
championship:rwSbupqsln,
uniform:[
first_colour,
second_colour,
third_colour
]
}



我使用以下命令在csv中导出文档:

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



这是我在输出中的csv:

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

csv文件告诉我制服有一些问题。
我必须如何设置命令中的统一字段以从MongoDB正确导出数组?

解决方案

是目前可能的。我认为你最好的打赌是使用统一字段,而不引用元素索引。

  mongoexport  - 数组中的所有值将被写入csv文件,您可以在导出后正确地重新格式化它们。 -db paul --collection team --type = csv --fields _id,official_name,common_name,country,championship,uniform --outC:\path\to\outputFile\output.csv


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?

解决方案

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"

这篇关于从MongoDB导出csv中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆