How do you rename a MongoDB database?

In this tutorial, we will learn How to rename a MongoDB database. However, there is no such direct method available just like MySQL to rename the existing database but there is always a second way to do so and we complete using some specific Steps that we will learn below.

Rename

Rename The Database In MongoDB

As we discussed above although there is no direct way to rename the existing database but we can always create a new database with the desired name.

Perform the same in different ways following the example given below.

There are five steps that need to be followed so let’s look.

Step 1. Create a database of names that you want to keep for the database you want

use new_database_name

Here we created a database of name new_database_name to be used further.

Step 2. Now we need to copy the existing database and store all the data of the database in the newly created database. To do so follow the code below.

db.copyDatabase("old_database_name", "new_database_name");

It will copy the old database to the new database whose name is as per our requirements.

Step 3.  Check whether the whole data that we want is copied to the new database or not it is done just to verify the whole data which we can do as followed.

use new_database_name
db.getCollectionNames() Or show Collections()

Now to check whether all the collections are copied or now we can follow two different ways as shown.

Step 4. ow we may delete the old database as it is of no use now as the whole data is stored in the new database, to do so follow the example

use old_database_name
db.dropDatabase()

It will drop the database.

To learn more about How do you rename a MongoDB database? at MongoDB Community

And to learn more about java you may visit MongoDB Tutorial and Problems

And for learning other programming languages you can also visit Beta Python  or solving the problems.

Leave a Comment

%d bloggers like this: