How to rename a database in mongoDB?

Viewed 5631

I want to rename the database that I've created in mongodb shell, however I used db.copyDatabase function but it did not work it gave an error saying

WARNING: db.copyDatabase will only function with MongoDB 4.0 and below. See http://dochub.mongodb.org/core/4.2-copydb-clone                                                                                        {                                                                                                                                                                                                                          "ok" : 0,                                                                                                                                                                                                          "errmsg" : "no such command: 'copydb'",                                                                                                                                                                            "code" : 59,                                                                                                                                                                                                       "codeName" : "CommandNotFound"                                                                                                                                                                             }

Please can anyone suggest which command should be used to rename the database?

2 Answers

That's a deprecation warning. Meaning that feature will be removed in near future. It seems you are using version higher than 4.0

As mentioned in the documentation,

Starting in version 4.2, MongoDB removes the copydb command. The deprecated db.copyDatabase(), which wraps the copydb command, can only be run against MongoDB 4.0 or earlier versions. For behavior and examples, refer to the 4.0 or earlier version of the manual.

For an alternative in version 4.2+, see Copy/Clone a Database.

Refer documentation

To use it above 4.0 versions

I had encountered the same issue in v4.4. So I had used mongodump -d oldDBname path then used mongorestore -d NewDBname path/oldDBname.bson and then dropped the Old DB with db.dropDatabase();

Related