How to update the name of a Database in MongoDB?
Ask by Steilla Mitchel
Updated 26 Aug 2023
You can update the name of a database in MongoDB using the
renameDatabase()method. The syntax for therenameDatabase()method is as follows:Where
oldNameis the name of the database that you want to rename andnewNameis the new name of the database.For example, the following code would rename the database
mydbtonewdb: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
oldNameis the name of the database that you want to copy andnewNameis the new name of the database.For example, the following code would copy the database
mydbtonewdb:The
copyDatabase()method will create a new database callednewdband copy all of the data from the databasemydbto the new database.