Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export a csv from mongodb

I have two collections in my mongodb namely

1.companies

2.contacts

Both the companies and contact collection are interlinked. I want to export a particular companies contact into a csv. I have tried a mongo export command as follows

 mongoexport --csv -d dbname -c contacts 
 -q {"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}; 
 -f {"first_name","last_name","title"} -o export.csv

I get a error as follows

SyntaxError: missing ; before statement (shell):1.

Please help me. Thanks in Advance

like image 756
SRIRAM Avatar asked Nov 27 '12 04:11

SRIRAM


2 Answers

There could be a couple of things going on here. First, are you running mongoexport from the command line or from the mongo shell? The mongoexport command is run from the command line.

Secondly, you need to properly format the query and field parameters. You could enclose the query with single quotes, and the filed name is not a JSON document, but just a list of fields.

This would look like the following from the command line:

mongoexport --csv -d dbname -c contacts -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f "first_name","last_name","title" -o export.csv
like image 52
Andre de Frere Avatar answered Nov 15 '22 18:11

Andre de Frere


The following query will work if it is running from commandLine

mongoexport -h host -d dbname -c contacts --csv -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f first_name,last_name,title -o export.csv
like image 28
minhas23 Avatar answered Nov 15 '22 19:11

minhas23