在我的MongoDB数据库名称中有一个错别字,我正在寻找重命名数据库。
我可以像这样复制和删除…
db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();
是否有重命名数据库的命令?
在我的MongoDB数据库名称中有一个错别字,我正在寻找重命名数据库。
我可以像这样复制和删除…
db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();
是否有重命名数据库的命令?
当前回答
不,没有。见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...
其他回答
尽管Mongodb没有提供rename Database命令,但它提供了rename Collection命令,该命令不仅修改了集合名称,还修改了数据库名称。
{ renameCollection: "<source_namespace>", to: "<target_namespace>", dropTarget: <true|false> writeConcern: <document> }
db.adminCommand({renameCollection: "db1.test1", to: "db2.test2"})
此命令只修改元数据,成本非常小,只需要遍历db1下的所有集合,重命名为db2即可实现重命名数据库名称。 你可以在这个Js脚本中完成
var source = "source";
var dest = "dest";
var colls = db.getSiblingDB(source).getCollectionNames();
for (var i = 0; i < colls.length; i++) {
var from = source + "." + colls[i];
var to = dest + "." + colls[i];
db.adminCommand({renameCollection: from, to: to});
}
请谨慎使用该命令
renameCollection具有不同的性能影响,具体取决于目标名称空间。 如果目标数据库与源数据库相同,则 renameCollection只是更改名称空间。这是一个快速的 操作。 如果目标数据库与源数据库不同, renameCollection将源集合中的所有文档复制到 目标集合。根据集合的大小,这个 可能需要更长的时间才能完成。
我试过了。
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/
从4.2版开始,copyDatabase已弃用。从现在开始,我们应该使用:mongump和mongorestore。
假设我们有一个名为:old_name的数据库,我们希望将其重命名为new_name。
首先,我们必须转储数据库:
mongodump --archive="old_name_dump.db" --db=old_name
如果您必须作为用户进行身份验证,则使用:
mongodump -u username --authenticationDatabase admin \
--archive="old_name_dump.db" --db=old_name
现在我们将db转储为一个名为:old_name_dump.db的文件。
用新名称恢复:
mongorestore --archive="old_name_dump.db" --nsFrom="old_name.*" --nsTo="new_name.*"
同样,如果您需要进行身份验证,请将以下参数添加到命令中:
-u username --authenticationDatabase admin
参考:https://docs.mongodb.com/manual/release-notes/4.2-compatibility/ # remove-support-for-the-copydb-and-clone-commands
不,没有。见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...
mongoshdeprecederror: [COMMON-10003] copyDatabase()已被移除,因为它在MongoDB 4.0中已弃用