Last active
February 18, 2021 00:06
-
-
Save nestoru/5e9e98492567100dc9c6a2156f95bcfc to your computer and use it in GitHub Desktop.
Revisions
-
nestoru revised this gist
Feb 18, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ function fail() { echo "mongo-collection-to-csv.sh authdb user collection csvpath host" echo echo "Example:" echo "export MONGODB_PWD=mypwd && ./mongo-collection-to-csv.sh admin mydb myuser mycollection /tmp/mycollection.csv 'cluster0-shard-00-00-10xyz.mymongo.net:27017,cluster0-shard-00-01-10xyz.mymongo.sample.com:27017,cluster0-shard-00-02-10xyz.mymongo.sample.com:27017'" exit 1 } -
nestoru created this gist
Feb 18, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ #!/bin/bash -e # mongo-collection-to-csv.sh # # description: Exports all field from any mongodb collection to CSV # author: Nestor Urquiza # date: 20210218 # env vars password=$MONGODB_PWD # functions function fail() { echo "Usage:" echo "export MONGODB_PWD=**** \\" echo "mongo-collection-to-csv.sh authdb user collection csvpath host" echo echo "Example:" echo "./mongo-collection-to-csv.sh admin mydb myuser mycollection /tmp/mycollection.csv 'cluster0-shard-00-00-10xyz.mymongo.net:27017,cluster0-shard-00-01-10xyz.mymongo.sample.com:27017,cluster0-shard-00-02-10xyz.mymongo.sample.com:27017'" exit 1 } function run() { OIFS=$IFS; IFS=","; echo 'exporting collection' ${collection} to ${csvpath} keys=`mongo "$host/$db" --ssl --authenticationDatabase $authdb -u $user -p"$password" --eval "rs.slaveOk(); var keys = []; for(var key in db.${collection}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys.join(',');" --quiet`; mongoexport --db=lms --collection=${collection} --type=csv --out=$csvpath -h "'$host'" -u $user -p"$password" --ssl --authenticationDatabase=admin --fields "'$keys'" IFS=$OIFS; } # main # arguments authdb=$1 db=$2 user=$3 collection=$4 csvpath=$5 host=$6 if [ "$MONGODB_PWD" == "" ] || [ "$authdb" == "" ] || [ "$db" == "" ] || [ "$user" == "" ] || [ "$collection" == "" ] || [ "$csvpath" == "" ] || [ "$host" == "" ]; then fail else run fi