Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongorestore to a different database

In MongoDB, is it possible to dump a database and restore the content to a different database? For example like this:

mongodump --db db1 --out dumpdir mongorestore --db db2 --dir dumpdir 

But it doesn't work. Here's the error message:

building a list of collections to restore from dumpdir dir

don't know what to do with subdirectory "dumpdir/db1", skipping...

done

like image 546
Cyker Avatar asked Mar 31 '16 00:03

Cyker


People also ask

How do you use Mongorestore?

Basic mongorestore syntax The basic way to restore a database is to use the mongorestore command to specify the backup directory (dump directory) without any options. This option is suitable for databases located in the localhost (127.0. 0.1) using the port 27017.

Does Mongorestore overwrite data?

Mongorestore only uses insert commands and does not execute any modifications when restoring data into an existing database. As a result, the restoration process will not overwrite existing documents that have a matching value for the _id field of the documents in the backup.

How do I restore a single database in MongoDB?

To restore a single database or a collection (or specific documents) from a snapshot, you can use the Queryable Backup to export a single database or collection to restore to the target deployment.

How do I drop a database in MongoDB?

The best way to do it is from the mongodb console: > use mydb; > db. dropDatabase(); Alternatively, you can stop mongod and delete the data files from your data directory, then restart.


2 Answers

You need to actually point at the "database name" container directory "within" the output directory from the previous dump:

mongorestore -d db2 dumpdir/db1 

And usually just <path> is fine as a positional argument rather than with -dir which would only be needed when "out of position" i.e "in the middle of the arguments list".

p.s. For archive backup file (tested with mongorestore v3.4.10)

mongorestore --gzip --archive=${BACKUP_FILE_GZ} --nsFrom "${DB_NAME}.*" --nsTo "${DB_NAME_RESTORE}.*" 
like image 192
Blakes Seven Avatar answered Sep 20 '22 06:09

Blakes Seven


mongodump --db=DB_NAME --out=/path-to-dump mongorestore --nsFrom "DB_NAME.*" --nsTo "NEW_DB_NAME.*" /path-to-dump 
like image 41
Mostafa Nazari Avatar answered Sep 22 '22 06:09

Mostafa Nazari