How to update the name of a Database in MongoDB?
How to update the name of a Database in MongoDB?
130
24-Aug-2023
Aryan Kumar
26-Aug-2023You can update the name of a database in MongoDB using the
renameDatabase()
method. The syntax for therenameDatabase()
method is as follows:Where
oldName
is the name of the database that you want to rename andnewName
is the new name of the database.For example, the following code would rename the database
mydb
tonewdb
:It is important to note that the
renameDatabase()
method does not move the data in the database. It simply changes the name of the database.If you want to move the data in the database, you can use the
copyDatabase()
method. The syntax for thecopyDatabase()
method is as follows:Where
oldName
is the name of the database that you want to copy andnewName
is the new name of the database.For example, the following code would copy the database
mydb
tonewdb
:The
copyDatabase()
method will create a new database callednewdb
and copy all of the data from the databasemydb
to the new database.