How to rename a MongoDB database

06 May,2021 by Jack Vamvas

Question: I've looked through the documentation to identify whether it is possible to  execute an a rename database command for a MongoDB database. I couldn't find a command to commit a straightforward rename . Something similar to:

MymongoClient.renameDatabase("oldDBName","newDBName");

What is a straightforward method to rename a MongoDB database?

 

Answer: You are right - at the time of this post there isn't a command to rename an existing database. I have read on various sources that MongoDB are working on it .Prior to MongoDB 4 -and starting in version 4.2, MongoDB removes the deprecated copydb command and clone command.

The corresponding mongo shell helpers db.copyDatabase() and db.cloneDatabase() can only be run when connected to MongoDB 4.0 or earlier.

 

The MongoDB recommended method is to use the mongodump & mongorestore methods

Method  - Mongodump & MongoRestore 

--dump out all the object

mongodump -d oldDBname path

--restore the dumped out objects

mongorestore -d NewDBname path/oldDBname.bson

--drop the old database

Drop the Old DB with db.dropDatabase();

 

For more on MongoDB commands Quick Access To Mongodb Commands Cheat Sheet

 


Author: Jack Vamvas (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to rename a MongoDB database


dba-ninja.com