Anand Abhay
Anand Abhay

Reputation: 359

Mongoexport with Query using shell script

I am calling mongoexport using shell script but it keeps failing. My script is :--

mongo localhost:27038/admin -u test -p mongo123 < mongoexport.js

and my mongoexport.js file is :--

use db1
mongoexport --type=csv --fields _id -q '{"_id": "323549991" , "cat" : "ABC"}' --out report.csv

But every time I run it fails with below error :--

switched to db db1
2018-01-10T17:36:15.495+0000 E QUERY    [thread1] SyntaxError: missing ;   before statement @(shell):1:14 
bye

Now I am not sure where exactly I am messing up the syntax .

Regards.

Upvotes: 0

Views: 1008

Answers (1)

Alex P.
Alex P.

Reputation: 3171

It looks like you are connecting to your mongo. You don't need to do that in order to execute mongoexport. You just need to connect to your host (not mongo). Take a look at the official documentation

This data resides on the MongoDB instance located on the host mongodb1.example.net running on port 37017, which requires the username user and the password pass.

mongoexport --host mongodb1.example.net --port 37017 --username user --password "pass" --collection contacts --db marketing --out mdb1-examplenet.json

In your case it should look like that (Untested)

mongoexport --host localhost --port 27038 --username test --password "mongo123" --db admin --collection db1 --type=csv --fields _id -q '{"_id": "323549991" , "cat" : "ABC"}' --out report.csv

I assumed your database is called admin and your collection db1, if not replace them accordingly.

Upvotes: 1

Related Questions