mongoexport csv 输出数组值

mongoexport csv 输出数组值

我正在使用 mongoexport 将一些集合导出到 CSV 文件中,但是当我尝试定位数组成员的字段时,我无法正确导出它。

我正在使用的命令:

mongoexport -d db -c collection -fieldFile fields.txt --csv > out.csv

fields.txt 的内容类似于

id
name
address[0].line1
address[0].line2
address[0].city
address[0].country
address[0].postcode

BSON 数据如下:

{
    "id": 1,
    "name": "example",
    "address": [
        {
            "line1": "flat 123",
            "line2": "123 Fake St.",
            "city": "London",
            "country": "England",
            "postcode": "N1 1AA"
        }
    ]
}

导出数组内容的正确语法是什么?

答案1

你几乎是对的,试试这个:

id
name
address.0.line1
address.0.line2
address.0.city
address.0.country
address.0.postcode

我将您的示例文档插入到bar数据库的集合中test,然后像这样运行导出:

./mongoexport --port 31000 -d test -c bar -fieldFile fields.txt --csv > out.csv

然后检查了结果,我觉得结果不错:

cat out.csv
id,name,address.0.line1,address.0.line2,address.0.city,address.0.country,address.0.postcode
1.0,"example","flat 123","123 Fake St.","London","England","N1 1AA"

相关内容