Update database name and collection Name

how can i update Database name and collection name ?

1 Like

Hi @mansi_joshi

In the mongo shell, this helper could be useful

  • db.coll.renameCollection(here)

There is no method to rename database, as far as I searched, but this could be useful

db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();

Where you copy the database, and drop the old one.

Hello @Santiago_Miranda , thank you for your help!!!
but I also want know that why it not allow rename database directly instead of creating new one and drop old one.
Thank you in advance !!!

You’re welcome,

To be honest, that answer is over the top of my head, it’s related to how MongoDB stores databases’ metadata.

You can read this stackoverflow post though.

Also, apparently copying a database like that is very slow, so you should:

  1. mongodump the old database
  2. mongorestore with new name
  3. dropDatabase the old database

You’ll find how to in the same url I linked before.

For a more formal description, see the ticket in JIRA.

3 Likes

Hi @Santiago_Miranda,

Thanks for sharing these resources.

@mansi_joshi Please note that starting in MongoDB 4.2, we have removed the support for db.copyDatabase() command and you should use mongodump and mongorestore to copy/clone a database as mentioned by @Santiago_Miranda.

Let us know if you have any other doubts.

~ Shubham

1 Like