10

How do i change a db name in mongodb? Simple question but i can't find anything online on how to do this. I don't want to have to rebuild my entire database, i just simple want to change the name of it. thanks.

1

4 Answers 4

19

Simple use this :

db.copyDatabase("old_db_name","new_db_name","localhost")
use old_db_name
db.dropDatabase();
Sign up to request clarification or add additional context in comments.

2 Comments

Just be careful while renaming you must not mess up with copying the collections to existing database. Checkout my answer stackoverflow.com/a/44986052/1696621
Can be used upto 4.0 version.
10

The only way is to clone the database to one of a different name

http://www.mongodb.org/display/DOCS/Copy+Database+Commands

You could go here and vote for that feature

Comments

4

Starting in version 4.2, MongoDB removes the deprecated copydb command. Also, db.copyDatabase() and db.cloneDatabase() can only be run when connected to MongoDB 4.0 or earlier.

Use this approach for version 4.2 as mentioned at mongoDB manual

    1. Use mongodump to dump the test database to an archive mongodump-test-db:
mongodump --archive="mongodump-test-db" --db=test
    1. Use mongorestore with --nsFrom and --nsTo to restore (with database name change) from the archive:
mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'

Comments

3

You can dump database

./mongodump -d mydb

rename folder

mv /path/to/dump/folder/mydb /path/to/dump/folder/mynewdb

and then restore

./mongorestore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.