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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With