Created
August 21, 2014 19:55
-
-
Save guilhermesteves/29286eb6b039c669eb67 to your computer and use it in GitHub Desktop.
Exportar todas as coleções de um Mongo DB para CSVs (trocar o DBAQUI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OIFS=$IFS; | |
IFS=","; | |
dbname=DBAQUI | |
host=127.0.0.1:27017 | |
collections=`mongo "$host/$dbname" --eval "rs.slaveOk();db.getCollectionNames();"`; | |
collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`; | |
collectionArray=($collections); | |
for ((i=0; i<${#collectionArray[@]}; ++i)); | |
do | |
echo 'exporting collection' ${collectionArray[$i]} | |
keys=`mongo "$host/$dbname" --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`; | |
mongoexport --host $host -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv; | |
done | |
IFS=$OIFS; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment