Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm using mongoexport to export some collections into CSV files, however when I try to target fields which are members of an array I cannot get it to export correctly.

command I'm using:

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

and the contents of fields.txt is similar to

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

where the BSON data would be:

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

what is the correct syntax for exporting the contents of an array?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You're almost right, try this:

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

I inserted your sample document into collection bar in database test and then ran the export like this:

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

Then checked the results, which look good to me:

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.