How to Rename a Database in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. Rename a MongoDB Database Using MongoDB GUI
  2. Rename a MongoDB Database Using the MongoDB Shell
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.

  1. Rename a MongoDB database using MongoDB GUI
  2. 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.

  1. Copy the original database by copying all the collections, views, and buckets.
  2. Create a new database.
  3. Name the new database as desired.
  4. Paste the copied collections, views, and buckets into the new database.
  5. 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.

Rename a MongoDB database

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.

  1. Right-click on the users database.
  2. Click on Copy All Collections/Views/Buckets.

duplicate contents of the database

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.

  1. Right-click on the target server (Studio 3T Replica Set in our example).
  2. Choose Add Database....

create a new mongodb 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.

add database dialog where you create the database name

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.

  1. Right-click on the customers database.
  2. Choose Paste Collections/Views/Buckets.

paste the contents of the original database

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.

progress in the operations window

(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.

  1. Right-click on the original database.
  2. Choose Drop database.
  3. Click Drop database.

Studio 3T will display an error notice if you do not have the proper user permissions.

Drop the Original MongoDB Database

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.

Related Article - MongoDB Database