Had a quick task where I needed to export results of a mongo query into a CSV file.
Mongoexport command comes in very handy in this case.
I constructed my query:
mongoexport -h myhost.mongolab.com:PORT -d mydb -u myusername -p mypassword -c coll_name -q {tag:/mytagname/,count:{$gte:10}} -f tag,tag_date,count --csv -o test.csv
And when I ran it, got this error “too many positional options”
Some google groups suggest that you need to remove the space between -p and password, but it was actually fixed in the latest version.
The problem was caused by the lack quotes around my query – they need to be there. So the correct one looks like this:
mongoexport -h myhost.mongolab.com:PORT -d mydb -u myusername -p mypassword -c coll_name -q '{tag:/mytagname/,count:{$gte:10}}' -f tag,tag_date,count --csv -o test.csv
Also, “-f” values (for field names) need to be comma-separated, no spaces.
Thanks. This helped a lot. Couldn’t find the solution elsewhere.
Glad to hear – thanks!