Skip to content

Instantly share code, notes, and snippets.

@guilhermesteves
Created August 21, 2014 19:55
Show Gist options
  • Save guilhermesteves/29286eb6b039c669eb67 to your computer and use it in GitHub Desktop.
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)
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