How to export all collections in MongoDB?

前端 未结 28 1522
你的背包
你的背包 2020-11-29 14:32

I want to export all collections in MongoDB by the command:

mongoexport -d dbname -o Mongo.json

The result is:
No collection specifie

相关标签:
28条回答
  • 2020-11-29 14:44

    Please let us know where you have installed your Mongo DB ? (either in Ubuntu or in Windows)

    • For Windows:

      1. Before exporting you must connect to your Mongo DB in cmd prompt and make sure that you are able to connect to your local host.
      2. Now open a new cmd prompt and execute the below command,

      mongodump --db database name --out path to save
      eg: mongodump --db mydb --out c:\TEMP\op.json

      1. Visit https://www.youtube.com/watch?v=hOCp3Jv6yKo for more details.
    • For Ubuntu:

      1. Login to your terminal where Mongo DB is installed and make sure you are able to connect to your Mongo DB.
      2. Now open a new terminal and execute the below command,

      mongodump -d database name -o file name to save
      eg: mongodump -d mydb -o output.json

      1. Visit https://www.youtube.com/watch?v=5Fwd2ZB86gg for more details .
    0 讨论(0)
  • 2020-11-29 14:44

    I needed the Windows batch script version. This thread was useful, so I thought I'd contribute my answer to it too.

    mongo "{YOUR SERVER}/{YOUR DATABASE}" --eval "rs.slaveOk();db.getCollectionNames()" --quiet>__collections.txt
    for /f %%a in ('type __collections.txt') do @set COLLECTIONS=%%a
    for %%a in (%COLLECTIONS%) do mongoexport --host {YOUR SERVER} --db {YOUR DATABASE} --collection %%a --out data\%%a.json
    del __collections.txt
    

    I had some issues using set /p COLLECTIONS=<__collections.txt, hence the convoluted for /f method.

    0 讨论(0)
  • 2020-11-29 14:44

    I dump all collection on robo3t. I run the command below on vagrant/homestead. It's work for me

    mongodump --host localhost --port 27017 --db db_name --out db_path
    
    0 讨论(0)
  • 2020-11-29 14:46

    Here's what worked for me when restoring an exported database:

    mongorestore -d 0 ./0 --drop

    where ./contained the exported bson files. Note that the --drop will overwrite existing data.

    0 讨论(0)
  • 2020-11-29 14:46

    if you want to use mongoexport and mongoimport to export/import each collection from database, I think this utility can be helpful for you. I've used similar utility couple of times;

    LOADING=false
    
    usage()
    {
        cat << EOF
        usage: $0 [options] dbname
    
        OPTIONS:
            -h      Show this help.
            -l      Load instead of export
            -u      Mongo username
            -p      Mongo password
            -H      Mongo host string (ex. localhost:27017)
    EOF
    }
    
    while getopts "hlu:p:H:" opt; do
        MAXOPTIND=$OPTIND
    
        case $opt in 
            h)
                usage
                exit
                ;;
            l)
                LOADING=true
                ;;
            u)
                USERNAME="$OPTARG"
                ;;
            p) 
                PASSWORD="$OPTARG"
                ;;
            H)
                HOST="$OPTARG"
                ;;
            \?)
                echo "Invalid option $opt"
                exit 1
                ;;
        esac
    done
    
    shift $(($MAXOPTIND-1))
    
    if [ -z "$1" ]; then
        echo "Usage: export-mongo [opts] <dbname>"
        exit 1
    fi
    
    DB="$1"
    if [ -z "$HOST" ]; then
        CONN="localhost:27017/$DB"
    else
        CONN="$HOST/$DB"
    fi
    
    ARGS=""
    if [ -n "$USERNAME" ]; then
        ARGS="-u $USERNAME"
    fi
    if [ -n "$PASSWORD" ]; then
        ARGS="$ARGS -p $PASSWORD"
    fi
    
    echo "*************************** Mongo Export ************************"
    echo "**** Host:      $HOST"
    echo "**** Database:  $DB"
    echo "**** Username:  $USERNAME"
    echo "**** Password:  $PASSWORD"
    echo "**** Loading:   $LOADING"
    echo "*****************************************************************"
    
    if $LOADING ; then
        echo "Loading into $CONN"
        tar -xzf $DB.tar.gz
        pushd $DB >/dev/null
    
        for path in *.json; do
            collection=${path%.json}
            echo "Loading into $DB/$collection from $path"
            mongoimport $ARGS -d $DB -c $collection $path
        done
    
        popd >/dev/null
        rm -rf $DB
    else
        DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')
    
        mkdir /tmp/$DB
        pushd /tmp/$DB 2>/dev/null
    
        for collection in $DATABASE_COLLECTIONS; do
            mongoexport --host $HOST -u $USERNAME -p $PASSWORD -db $DB -c $collection --jsonArray -o $collection.json >/dev/null
        done
    
        pushd /tmp 2>/dev/null
        tar -czf "$DB.tar.gz" $DB 2>/dev/null
        popd 2>/dev/null
        popd 2>/dev/null
        mv /tmp/$DB.tar.gz ./ 2>/dev/null
        rm -rf /tmp/$DB 2>/dev/null
    fi
    
    0 讨论(0)
  • 2020-11-29 14:47

    If you are OK with the bson format, then you can use the mongodump utility with the same -d flag. It will dump all the collections to the dump directory (the default, can be changed via the -o option) in the bson format. You can then import these files using the mongorestore utility.

    0 讨论(0)
提交回复
热议问题