Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a shell script to automate csv export in mongo db

We have mongo db and in that we have a list of collections which i wanna export to csv using the mongoexport tool. I need to do this often and the names of the collections changes sometimes. So what i wanna do is create a shell script that i can just run and it will iterate over the collections in the mongo db and create csv files. Right now i have a script but its not automated for example i have the following in a script.

 mongoexport -d mydbname -c mycollname.asdno3rnknlasfkn.collection --csv -f field1,field2,field3,field4 -o mycollname.asdno3rnknlasfkn.collection.csv

In this all the elements will remain same except csv filename and the collection name where both are same.

So i wanna create a script which will

 show collections

then loop over the collection names retrieved and replace it in the export tool command.

like image 758
swordfish Avatar asked Feb 23 '23 18:02

swordfish


1 Answers

This can easily be done via the shell - don't know if the comments above refer to old versions of the mongo shell... Example:

echo 'show collections' | mongo dbname --quiet
like image 189
bryn Avatar answered Feb 26 '23 09:02

bryn