Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I need to copy my Mongo database along with its data. I have tried

db.copyDatabase( "Old_db", "new_db", "localhost" )

But the problem is it only copies a blank db, not with the previous data.

1 Answer

0 votes
by (108k points)

For your kind information, copyDatabase is now deprecated so now you can use mongodump and mongorestore. Say, for instance, you want to copy the ‘data1’ database from a local instance to ‘data2’ database on the same instance, so for that, you can write the following code:

mongodump --archive="mongodump-data1-db" --db=test

The above code will dump the ‘data1’ database to an archive mongodump-data1-db.

Then you will use mongorestore to restore from the archived database with the help of --nsFrom and --nsTo functions:

mongorestore --archive="mongodump-data1-db" --nsFrom='data1.*' --nsTo='data2.*'

If you wish to learn MongoDB, check out this MongoDB tutorial for free! 

Related questions

Browse Categories

...