How to use the dumped data by mongodump?

前端 未结 6 2041
無奈伤痛
無奈伤痛 2020-12-04 15:03

I have used mongodump to dump my database of mongodb, it created some bson files under dump/mydb

But I don\'t know how to use them. I tried

相关标签:
6条回答
  • 2020-12-04 15:37

    I am using mongodump, mongorestore for daily backups and restoring from backup. I have two .bat files:
    First, for backup, where you need just specify host database name and backup folder:

    SET host=localhost:27020
    SET dbNameToDump=MyDB
    SET backupsFolder=Backups
    
    mongodump.exe --host %host% --db %dbNameToDump%
    
    SET date="%date:~10,4%-%date:~4,2%-%date:~7,2%.%time:~0,2%-%time:~3,2%"
    cd %backupsFolder%
    md %date%
    
    xcopy /e ..\dump %date%
    
    rmdir /s /q ..\dump
    

    Above bat file create folder with name like this 2011-03-31.11-17(yyyy-MM-dd.hh-ss) in folder Backups with dumped collections from specified database. In files explorer it looks like so:

    enter image description here

    Second bat file i use for retore specified dumped files(here you also need specify database name and folder with dumped files):

    SET host=localhost:27020
    SET dbNameToRestore=MyDB
    SET restoreFolder=Restore
    
    mongorestore.exe --host %host% --db %dbNameToRestore% %restoreFolder%
    

    In files explorer:

    enter image description here

    In additional, i am using windows schedule to automate backup process.

    Hope above information will be useful for someone.

    0 讨论(0)
  • 2020-12-04 15:38

    You need to use mongorestore, not mongoimport ... which is used for things like importing json, or csv, etc.

    From the back-up-with-mongodump docs:

    mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database.

    mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.

    You can read more about mongorestore in the docs below; I'd take a look and read up on them as they are very helpful.

    http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore

    You can also check out http://learnmongo.com for tips and help!

    0 讨论(0)
  • 2020-12-04 15:47

    As mentioned in the previous answers, you have to use mongorestore instead of mongoimport. Adding to the previous answers, when your mongodb is running, execute the following command to restore your dump from the dump directory,

    mongorestore dump
    

    This will import all the collections into your mydb database. However this doesn't drop the database before restoring. If you wish to drop the database before importing,

    mongorestore --drop dump
    

    The bson files in the mydb directory will be restored as the collections inside mydb database. For more info on mongorestore check the documentation here.

    0 讨论(0)
  • 2020-12-04 15:53

    For mongo version 3 and above use the command below:

    mongorestore --host=localhost --port=27017 --username=root --authenticationDatabase=admin --db=test dump_folder/
    

    Mongo will ask password after that

    0 讨论(0)
  • 2020-12-04 15:56

    Use mongorestore. mongoimport works on the output of mongoexport. mongodump & mongorestore work on binary data files while import / export work on json, csv, etc.. (human readable formats)

    0 讨论(0)
  • 2020-12-04 16:04

    For resolving this, I copied the dump folder,dbdump(which contains bson files) to bin directory of mongodb and executed the below commands in command prompt:

    1. cd "path to MongoDB's bin folder"
    (Example: cd C:\Program Files\MongoDB\Server\3.2\bin)

    2. mongorestore.exe --dir ./directory name --db database-name
    (Example: mongorestore --dir ./dbdump --db testdb)

    All bson files in the dump folder will be imported into your database. You can verfiy this by executing the below commands :
    cd "path to MongoDB's bin folder"
    mongo.exe
    show dbs;

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