71

I created a backup of all my databases using mongodump command. Now I want to restore a specific database using mongorestore command.

How is this possible, I use this command: --db option then mongodb doesn't restore a specific database.

Michael Green
  • 24,839
  • 13
  • 51
  • 96
pankaj choudhary
  • 841
  • 2
  • 8
  • 4

6 Answers6

103

To restore a single database you need to provide the path to the dump directory as part of the mongorestore command line.

For example:

# Backup the training database
mongodump --db training

# Restore the training database to a new database called training2
mongorestore --db training2 dump/training

The --db option for mongodump specifies the source database to dump.

The --db option for mongorestore specifies the target database to restore into.

Stennie
  • 10,235
  • 2
  • 28
  • 46
20

Use the following command to restore mongo db:

mongorestore -d dbname dbpath
Tom V
  • 15,670
  • 7
  • 63
  • 86
user104596
  • 201
  • 2
  • 2
11
$ mongorestore --drop -d <database-name> <dir-of-backup-files>
  • --drop Drop is necessary if you are replacing an existing db
  • -d <database-name> The name of the database to create/replace
  • <dir-of-backup-files> For some reason this is necessary even if it's the current directory

enter image description here

Reference: https://coderwall.com/p/3hx06a/restore-a-mongodb-database-mongorestore

doub1ejack
  • 329
  • 3
  • 11
1

Restoring using mongorestore

After making sure that mongod and mongo are running, go to the dump's parent directory in a new terminal. And use the command mongorestore dump. Where dump is the folder name in which the database dump is present.

Restoring using mongorestore

Zameer Ansari
  • 901
  • 1
  • 13
  • 19
0

Maybe slightly unrelated, but based on the answers here i got the following

Using mongodump and mongorestore archive feature I comprised a oneliner:

mongodump --host H --port P --username U --password PWD --archive | mongorestore --username U1 --password PWD1 --archive

The above example will :

  1. Dump data from host H with port P logged in with user U and password PWD
  2. "Stream" that data into your local database with user U1 and password PWD1
0

In case you are restoring from an archive, --db won't work.

From the mongodb docs:

The use of --db and --collection options are deprecated when restoring from a directory or an archive file.

So if you are restoring from an archive (--archive) you should add this:

--nsFrom="ORIGINAL_DB_NAME.*" --nsTo="NEW_DB_NAME.*" 
mustaccio
  • 25,896
  • 22
  • 57
  • 72
Iaron
  • 1