在我的MongoDB数据库名称中有一个错别字,我正在寻找重命名数据库。

我可以像这样复制和删除…

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

是否有重命名数据库的命令?


当前回答

我试过了。

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

然后才知道它已经被mongo社区弃用了,尽管它创建了备份或重命名了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
}

所以不相信上面的方法,我不得不采取转储本地使用下面的命令

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

连接到Db。

use DB_TobeRenamed

然后

db.dropDatabase()

然后使用命令恢复数据库。

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

其他回答

不,没有。见https://jira.mongodb.org/browse/server - 701

Unfortunately, this is not an simple feature for us 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 This is just to accomplish a rename of a single database in a standalone mongod instance. For replica sets the above would need to be done on every replica node, plus on each node every single oplog entry that refers this database would have to be somehow invalidated or rewritten, and then if it's a sharded cluster, one also needs to add these changes to every shard if the DB is sharded, plus the config servers have all the shard metadata in terms of namespaces with their full names. There would be absolutely no way to do this on a live system. To do it offline, it would require re-writing every single database file to accommodate the new name, and at that point it would be as slow as the current "copydb" command...

上面的过程很慢,你可以使用下面的方法,但你需要一个集合一个集合地移动到另一个db。

use admin
db.runCommand({renameCollection: "[db_old_name].[collection_name]", to: "[db_new_name].[collection_name]"})

mongoshdeprecederror: [COMMON-10003] copyDatabase()已被移除,因为它在MongoDB 4.0中已弃用

在将所有数据放入管理数据库的情况下(您不应该这样做),您将注意到db.copyDatabase()将不起作用,因为您的用户需要许多特权,而您可能不想给予它。下面是一个手动复制数据库的脚本:

use old_db
db.getCollectionNames().forEach(function(collName) {
    db[collName].find().forEach(function(d){
        db.getSiblingDB('new_db')[collName].insert(d); 
    }) 
});

注意:希望在最新版本中有所改变。 不能在MongoDB 4.0 mongod实例之间复制数据 FCV值)和MongoDB 3.4和更早的mongod实例。 https://docs.mongodb.com/v4.0/reference/method/db.copyDatabase/

警告:嘿,伙计们,在复制数据库时要小心,如果你不想在一个数据库下搞砸不同的集合。

下面展示了如何重命名

> show dbs;
testing
games
movies

要重命名,请使用以下语法

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

例子:

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

现在,您可以通过以下方式安全地删除旧的db

use testing;

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

现在,如果尝试用现有数据库名称重命名新数据库名称,会发生什么情况

例子:

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

因此,在这种情况下,所有的测试集合(表)将被复制到电影数据库。