How do you rename a MongoDB database?

前端 未结 10 1560
难免孤独
难免孤独 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:07

    I tried doing.

    db.copyDatabase('DB_toBeRenamed','Db_newName','host') 
    

    and came to know that it has been Deprecated by the mongo community although it created the backup or renamed DB.

    WARNING: db.copyDatabase is deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation
    {
            "note" : "Support for the copydb command has been deprecated. See 
            http://dochub.mongodb.org/core/copydb-clone-deprecation",
            "ok" : 1
    }
    

    So not convinced with the above approach I had to take Dump of local using below command

    mongodump --host --db DB_TobeRenamed --out E://FileName/
    

    connected to Db.

    use DB_TobeRenamed
    

    then

    db.dropDatabase()
    

    then restored the DB with command.

    mongorestore -host hostName -d Db_NewName E://FileName/
    

提交回复
热议问题