How to export all collections in MongoDB?

前端 未结 28 1524
你的背包
你的背包 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 15:03

    You can use mongo --eval 'printjson(db.getCollectionNames())' to get the list of collections and then do a mongoexport on all of them. Here is an example in ruby

      out = `mongo  #{DB_HOST}/#{DB_NAME} --eval "printjson(db.getCollectionNames())"`
    
      collections = out.scan(/\".+\"/).map { |s| s.gsub('"', '') }
    
      collections.each do |collection|
        system "mongoexport --db #{DB_NAME}  --collection #{collection}  --host '#{DB_HOST}' --out #{collection}_dump"
      end
    
    0 讨论(0)
  • 2020-11-29 15:03

    If you're dealing with remote databases you can try these commands given that you don't mind the output being BSON

    1. Dump out as a gzip archive

    mongodump --uri="mongodb://YOUR_USER_ID:YOUR_PASSWORD@YOUR_HOST_IP/YOUR_DB_NAME" --gzip --archive > YOUR_FILE_NAME
    

    2. Restore (Copy a database from one to another)

    mongorestore --uri="mongodb://$targetUser:$targetPwd@$targetHost/$targetDb" --nsFrom="$sourceDb.*" --nsTo="$targetDb.*" --gzip --archive
    
    0 讨论(0)
  • 2020-11-29 15:04

    You can do it using the mongodump command

    Step 1 : Open command prompt

    Step 2 : go to bin folder of your mongoDB installation (C:\Program Files\MongoDB\Server\4.0\bin)

    Step 3 : then execute the following command

    mongodump -d your_db_name -o destination_path

    your_db_name = test

    destination_path = C:\Users\HP\Desktop

    Exported files will be created in destination_path\your_db_name folder (in this example C:\Users\HP\Desktop\test)

    References : o7planning

    0 讨论(0)
  • 2020-11-29 15:04

    If you want, you can export all collections to csv without specifying --fields (will export all fields).

    From http://drzon.net/export-mongodb-collections-to-csv-without-specifying-fields/ run this bash script

    OIFS=$IFS;
    IFS=",";
    
    # fill in your details here
    dbname=DBNAME
    user=USERNAME
    pass=PASSWORD
    host=HOSTNAME:PORT
    
    # first get all collections in the database
    collections=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();db.getCollectionNames();"`;
    collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`;
    collectionArray=($collections);
    
    # for each collection
    for ((i=0; i<${#collectionArray[@]}; ++i));
    do
        echo 'exporting collection' ${collectionArray[$i]}
        # get comma separated list of keys. do this by peeking into the first document in the collection and get his set of keys
        keys=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`;
        # now use mongoexport with the set of keys to export the collection to csv
        mongoexport --host $host -u $user -p $pass -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;
    done
    
    IFS=$OIFS;
    
    0 讨论(0)
  • 2020-11-29 15:04

    I realize that this is quite an old question and that mongodump/mongorestore is clearly the right way if you want a 100% faithful result, including indexes.

    However, I needed a quick and dirty solution that would likely be forwards and backwards compatible between old and new versions of MongoDB, provided there's nothing especially wacky going on. And for that I wanted the answer to the original question.

    There are other acceptable solutions above, but this Unix pipeline is relatively short and sweet:

    mongo --quiet mydatabase --eval "db.getCollectionNames().join('\n')" | \
    grep -v system.indexes | \
    xargs -L 1 -I {} mongoexport -d mydatabase -c {} --out {}.json
    

    This produces an appropriately named .json file for each collection.

    Note that the database name ("mydatabase") appears twice. I'm assuming the database is local and you don't need to pass credentials but it's easy to do that with both mongo and mongoexport.

    Note that I'm using grep -v to discard system.indexes, because I don't want an older version of MongoDB to try to interpret a system collection from a newer one. Instead I'm allowing my application to make its usual ensureIndex calls to recreate the indexes.

    0 讨论(0)
  • 2020-11-29 15:04
    1. Open the Connection
    2. Start the server
    3. open new Command prompt

    Export:

    mongo/bin> mongoexport -d webmitta -c domain -o domain-k.json

    Import:

    mongoimport -d dbname -c newCollecionname --file domain-k.json

    Where

    webmitta(db name)
    domain(Collection Name)
    domain-k.json(output file name)
    
    0 讨论(0)
提交回复
热议问题