Introduction
Renaming a database should be used with extreme caution. Be sure to backup the database first and confirm the new database before removing the old database.
Requirements
- MongoDB 2.6
Rename with Database Copy
Connect to MongoDB using the mongo shell.
mongo
The old database content can then be copied to a new database.
db.copyDatabase('old_database', 'new_database')
It is recommended that you verify the copy was successful before removing the old database. Once done, the old database can be removed.
use old_database
db.dropDatabase()
Rename with Backup/Restore
The mongodump and mongorestore commands can be used to backup the existing database and then restored into a database with the new name.
mongodump old_database
mongorestore --db new_database ./dump/old_database
The old database can be removed once a successful copy has been confirmed. Connect to MongoDB using the mongo shell.
mongo
The old database can then be removed.
use old_database
db.dropDatabase()
Views: 43