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?
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)
As mentioned in the previous answers, you have to use
mongorestore
instead ofmongoimport
. Adding to the previous answers, when your mongodb is running, execute the following command to restore your dump from the dump directory,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,
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.
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;
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:
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):
In files explorer:
In additional, i am using windows schedule to automate backup process.
Hope above information will be useful for someone.
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: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!