How to export all collections in MongoDB?

前端 未结 28 1525
你的背包
你的背包 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:56

    If you want to dump all collections in all databases (which is an expansive interpretation of the original questioner's intent) then use

    mongodump
    

    All the databases and collections will be created in a directory called 'dump' in the 'current' location

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

    If you want to backup all the dbs on the server, without having the worry about that the dbs are called, use the following shell script:

    #!/bin/sh
    
    md=`which mongodump`
    pidof=`which pidof`
    mdi=`$pidof mongod`
    dir='/var/backup/mongo'
    
    if [ ! -z "$mdi" ]
       then
            if [ ! -d "$dir" ]
               then
                   mkdir -p $dir
               fi
            $md --out $dir >/dev/null 2>&1
       fi
    

    This uses the mongodump utility, which will backup all DBs if none is specified.

    You can put this in your cronjob, and it will only run if the mongod process is running. It will also create the backup directory if none exists.

    Each DB backup is written to an individual directory, so you can restore individual DBs from the global dump.

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

    For lazy people, use mongodump, it's faster:

    mongodump -d <database_name> -o <directory_backup>
    

    And to "restore/import" it (from directory_backup/dump/):

    mongorestore -d <database_name> <directory_backup>
    

    This way, you don't need to deal with all collections individually. Just specify the database.

    Note that I would recommend against using mongodump/mongorestore for big data storages. It is very slow and once you get past 10/20GB of data it can take hours to restore.

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

    First, of Start the Mongo DB - for that go to the path as ->

    C:\Program Files\MongoDB\Server\3.2\bin and click on the mongod.exe file to start MongoDB server.

    Command in Windows to Export

    • Command to export MongoDB database in Windows from "remote-server" to the local machine in directory C:/Users/Desktop/temp-folder from the remote server with the internal IP address and port.

    C:> mongodump --host remote_ip_address:27017 --db -o C:/Users/Desktop/temp-folder

    Command in Windows to Import

    • Command to import MongoDB database in Windows to "remote-server" from local machine directory C:/Users/Desktop/temp-folder/db-dir

    C:> mongorestore --host=ip --port=27017 -d C:/Users/Desktop/temp-folder/db-dir

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

    To export in JSON format do this by following commands which you can see.

    mongoexport --db dbname --collection collectionName --out directoryPATH/JSONfileName.json
    
    0 讨论(0)
  • 2020-11-29 15:03

    Exporting all collections using mongodump use the following command

    mongodump -d database_name -o directory_to_store_dumps

    To restore use this command

    mongorestore -d database_name directory_backup_where_mongodb_tobe_restored
    
    0 讨论(0)
提交回复
热议问题