How do you rename a MongoDB database?

前端 未结 10 1563
难免孤独
难免孤独 2020-11-29 15:04

There\'s a typo in my MongoDB database name and I\'m looking to rename the database.

I can copy and delete like so...

db.copyDatabase(\'old_name\', \         


        
10条回答
  •  忘掉有多难
    2020-11-29 15:18

    NOTE: Hopefully this changed in the latest version.

    You cannot copy data between a MongoDB 4.0 mongod instance (regardless of the FCV value) and a MongoDB 3.4 and earlier mongod instance. https://docs.mongodb.com/v4.0/reference/method/db.copyDatabase/

    ALERT: Hey folks just be careful while copying the database, if you don't want to mess up the different collections under single database.

    The following shows you how to rename

    > show dbs;
    testing
    games
    movies
    

    To rename you use the following syntax

    db.copyDatabase("old db name","new db name")
    

    Example:

    db.copyDatabase('testing','newTesting')
    

    Now you can safely delete the old db by the following way

    use testing;
    
    db.dropDatabase(); //Here the db **testing** is deleted successfully
    

    Now just think what happens if you try renaming the new database name with existing database name

    Example:

    db.copyDatabase('testing','movies'); 
    

    So in this context all the collections (tables) of testing will be copied to movies database.

提交回复
热议问题