6

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

3
  • Did u try looking into the mongodb documentation Commented Nov 27, 2012 at 4:12
  • @AbhayKumar yes from that only i found about mongo export. Commented Nov 27, 2012 at 4:13
  • can you please paste some of your data form the collection Commented Nov 27, 2012 at 4:31

2 Answers 2

19

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
Sign up to request clarification or add additional context in comments.

6 Comments

assertion: 10340 Failure parsing JSON string near: '{employme..I got this error
what version are you using? Exporting a sample document with 2.2.1 returns no errors. Could you post a sample document - I've guessed at the structure in the format that is suggested by your command, but I could be incorrect.
I've just tried with 2.0.6 and also not seeing an error. Could you post the document you get from db.contacts.find({"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}) when you try it from the mongo shell?
thanks @andre de Frere I got the answer. I missed the backslash before double quotes
mongoexport --csv -d dbname -c contacts --query {\"employment_details.company_id\":ObjectId(\"50926cff9fe3125819006dc7\")} -f "first_name,last_name,title" -o export.csv
|
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.