QBoard » Big Data » Big Data - Data Storage : Hive, HBase, MongoDB, Teradata.. » How to export all collections in MongoDB?

How to export all collections in MongoDB?

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

    mongoexport -d dbname -o Mongo.json
    

     

    The result is:

    No collection specified!

    The manual says, if you don't specify a collection, all collections will be exported.

    However, why doesn't this work?

    http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection

    My MongoDB version is 2.0.6.

      August 16, 2021 1:50 PM IST
    0
  • I wrote bash script for that. Just run it with 2 parameters (database name, dir to store files).

    #!/bin/bash
    
    if [ ! $1 ]; then
            echo " Example of use: $0 database_name [dir_to_store]"
            exit 1
    fi
    db=$1
    out_dir=$2
    if [ ! $out_dir ]; then
            out_dir="./"
    else
            mkdir -p $out_dir
    fi
    
    tmp_file="fadlfhsdofheinwvw.js"
    echo "print('_ ' + db.getCollectionNames())" > $tmp_file
    cols=`mongo $db $tmp_file | grep '_' | awk '{print $2}' | tr ',' ' '`
    for c in $cols
    do
        mongoexport -d $db -c $c -o "$out_dir/exp_${db}_${c}.json"
    done
    rm $tmp_file
      December 24, 2021 1:16 PM IST
    0
  • Follow the steps below to create a mongodump from the server and import it another server/local machine which has a username and a password
    1. mongodump -d dbname -o dumpname -u username -p password 2. scp -r user@remote:~/location/of/dumpname ./ 3. mongorestore -d dbname dumpname/dbname/ -u username -p password
      September 6, 2021 5:56 PM IST
    0
  • In case you want to connect a remote mongoDB server like mongolab.com, you should pass connection credentials eg.

    mongoexport -h id.mongolab.com:60599 -u username -p password -d mydb -c mycollection -o mybackup.json
    
      January 8, 2022 2:44 PM IST
    0