How to Rename a Database in MongoDB
Through the help of this MongoDB tutorial article, you will learn how to rename a MongoDB database. You will go through two methods to achieve this.
The following methods rename the database in MongoDB.
- Rename a MongoDB database using MongoDB GUI
- Rename a MongoDB database using the MongoDB shell
Rename a MongoDB Database Using MongoDB GUI
The procedures below demonstrate how to rename a database in MongoDB using the MongoDB GUI.
- Copy the original database by copying all the collections, views, and buckets.
- Create a new database.
- Name the new database as desired.
- Paste the copied collections, views, and buckets into the new database.
- Drop the original database.
In this example, you will rename the database users
to customers.
(Step 1) Copy the Original MongoDB Database
You’ll notice a list of databases in the Connection Tree on the left-hand side once you’ve connected to a MongoDB instance.
Studio 3T’s sophisticated context menus eliminate the need to use the shell to conduct basic MongoDB CRUD tasks. All we have to do in this scenario is duplicate the contents of the users
database.
You can do this by following the steps below.
- Right-click on the
users
database. - Click on Copy All Collections/Views/Buckets.
If you don’t see any copy options, ensure you don’t have more than one node selected.
(Step 2) Create a New MongoDB Database
Next, you will create a new database where you can paste the copied content. To add a database, follow the following steps.
- Right-click on the target server (Studio 3T Replica Set in our example).
- Choose
Add Database...
.
(Step 3) Name the New Database
The Add Database
dialog will pop up. You can name the newly-created database customers
and click OK
.
The customers
database will then pop up in the Connection Tree.
(Step 4) Paste the Contents of the Original Database
Now that you have created the new database, you will paste the contents of the original database users
into customers
by following the given steps below.
- Right-click on the
customers
database. - Choose Paste Collections/Views/Buckets.
The amount of time this process will take is determined by the size of your original database. The progress always is tracked in the Operations window, which is located below the Connection Tree on the bottom-left hand side.
(Step 5) Drop the Original MongoDB Database
After ensuring that everything has been correctly copied into the new database, you can drop the original database, users
.
- Right-click on the original database.
- Choose Drop database.
- Click Drop database.
Studio 3T will display an error notice if you do not have the proper user permissions.
The original database should vanish from the Connection Tree.
Rename a MongoDB Database Using the MongoDB Shell
Another method that can be used to rename a MongoDB database is through using the MongoDB shell. Before renaming the database, make sure that you back up the database first and confirm the new database before removing the old database.
Rename With Database Copy
Connect to MongoDB using the mongo shell.
mongo
The old database content can be copied to a new database.
db.copyDatabase('old_database', 'new_database')
Before deleting the old database, it’s good to double-check if the copy was successful. The old database can be deleted.
use old_database
db.dropDatabase()
Rename With Backup/Restore
The commands mongodump
and mongorestore
can back up the existing database and then restore it into a database with the new name.
mongodump old_database
mongorestore --db new_database ./dump/old_database
Once a copy has been confirmed, the old database can be removed. Connect to MongoDB using the mongo shell.
mongo
The old database will be removed.
use old_database
db.dropDatabase()
Through the help of this MongoDB article, we have learned how to rename a MongoDB database using different methods. These methods are renaming a MongoDB database using MongoDB GUI and renaming a MongoDB database using MongoDB shell.