Mongodb export nested field

Steven Filipowicz picture Steven Filipowicz · Jan 14, 2014 · Viewed 11.1k times · Source

I have a MongoDB and I want to export to a .csv file.

document:

{
  "id" : 28,
  "organisation" : "Mickey Mouse company",
  "country" : "US",
  "contactpersons" : [{
      "title" : "",
      "typecontact" : "D",
      "mobilenumber" : "757784854",
      "firstname" : "Mickey",
      "lastname" : "Mouse",
      "emailaddress" : "[email protected]"
    }],
  "modifieddate" : "2013-11-21T16:04:49+0100"
}

I want to export all document and only want the field contactpersons.firstname and contactpersons.emailaddress

I use this commandline:

     mongoexport -o /tmp/export.csv -host dbmongo -port 27017 -db organisation -collection organisationa -u user -p password -csv  -fields 'contactpersons.0.firstname, contactpersons.0.emailaddress'

This works more or less, it exports but only exports the field firstname and not emailaddress. I need it also the export the field emailaddress.

Any idea how I can do this? I don't understand why it doesn't work even though I do give the emailaddress field. Do error is given.

Thanks for any help!

Answer

Steven Filipowicz picture Steven Filipowicz · Jan 14, 2014

Found it. I needed to remove the spaces.

This is wrong:

mongoexport -o /tmp/export.csv -host dbmongo -port 27017 -db organisation -collection organisationa -u user -p password -csv  -fields 'contactpersons.0.firstname, contactpersons.0.emailaddress'

This is correct:

mongoexport -o /tmp/export.csv -host dbmongo -port 27017 -db organisation -collection organisationa -u user -p password -csv  -fields 'contactpersons.0.firstname,contactpersons.0.emailaddress'