mongodb

dump

mongorestore

mongodump

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 mongoimport, but seems it can't import bson data. Then how to use these bson files? How to import them to another mongodb?

Solution 1

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!

Solution 2

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:

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:

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

Hope above information will be useful for someone.

Solution 3

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.

Solution 4

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)

Solution 5

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;

Solution 6

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