Skip to content

Instantly share code, notes, and snippets.

@nestoru
Last active February 18, 2021 00:06
Show Gist options
  • Save nestoru/5e9e98492567100dc9c6a2156f95bcfc to your computer and use it in GitHub Desktop.
Save nestoru/5e9e98492567100dc9c6a2156f95bcfc to your computer and use it in GitHub Desktop.

Revisions

  1. nestoru revised this gist Feb 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongo-collection-to-csv.sh
    Original 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 "./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'"
    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
    }

  2. nestoru created this gist Feb 18, 2021.
    44 changes: 44 additions & 0 deletions mongo-collection-to-csv.sh
    Original 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