How to use the dumped data by mongodump?
Asked Answered
H

6

71

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?

Hughhughes answered 31/3, 2011 at 5:4 Comment(0)
K
71

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!

Kannan answered 31/3, 2011 at 5:12 Comment(0)
P
38

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.

Palstave answered 31/3, 2011 at 8:30 Comment(2)
This was really useful. I needed some extra functionality (backing up to a network share). gist.github.com/adamb0mb/7191353Qualify
how much data do you think is safe for dump? According to the mongo document, a small data set is recommended, but I don't know how to define small?Grapeshot
M
10

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.

Miamiami answered 25/8, 2016 at 5:51 Comment(2)
Strange, I tried to upvote your answer and it didn't change. Anyway, mongorestore needs to be run from command line, not in the mongo shellShoshanashoshanna
Oh yes, I forgot to mention that.Miamiami
G
6

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)

Grati answered 31/3, 2011 at 5:8 Comment(0)
C
3

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;

Charry answered 20/4, 2016 at 19:9 Comment(0)
I
1

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

Implicative answered 15/10, 2019 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.