Rename database in mongoDB

You are here : Home / MongoDB Tutorial


Contents of page >
STEP 1 > Create copy of old database.
STEP 2 > Now, drop old database.


1) As of now, mongoDB does not provide direct feature of renaming database in mongoDB >


“Unfortunately, this is not an simple feature for to implement due to the way that database metadata is stored in the original (default) storage engine. In MMAPv1 files, the namespace (e.g.: dbName.collection) that describes every single collection and index includes the database name, so to rename a set of database files, every single namespace string would have to be rewritten. This impacts:
  • the .ns file
  • every single numbered file for the collection
  • the namespace for every index
  • internal unique names of each collection and index
  • contents of system.namespaces and system.indexes (or their equivalents in the future)
  • other locations I may be missing”
See, jira.mongodb for detail.


2) So, we can use some alternative way to rename database in MongoDB >


STEP 1 > Create copy of old database.


db.copyDatabase("mydb","mydbCopy","localhost")

Mydb - is name of old-database.
mydbCopy - is the copy of old-database.
localhost - provided mongoDb server is running on local machine


STEP 2 > Now, drop old database.
use db_to_rename
db.dropDatabase();
See, more on how to Drop database in mongoDB

3) Summary -
As of now mongoDB does not provide direct method/way for renaming database in mongoDB.


So, we can use some alternative way to rename database in MongoDB >
First, Create copy of old database.
db.copyDatabase("mydb","mydbCopy","localhost")
Then, drop old database.
use db_to_rename
db.dropDatabase();


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

Update documents in collection in mongoDB

Delete documents in collection (table) in mongoDB

How to Delete all documents from collection in MongoDB

Install and connect to MongoDB on windows

Create new database in mongoDB

See list of all databases in MongoDb, switch database

Create new collection(table) in mongoDB


Labels: MongoDB
eEdit
Must read for you :