在我的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...

其他回答

不,没有。见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 < 4.2 (ref):

db.copyDatabase("db_to_rename","db_renamed","localhost")
use db_to_rename
db.dropDatabase();

编者按:这与问题本身使用的方法相同,但已被证明对其他人有用。

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

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

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

是的,这一切看起来都很混乱,其他答案似乎不适合我——大概是因为对工具进行了更多的更改——或者因为我使用的是旧版本。我点击上面的https://stackoverflow.com/a/63372970/1892584,直到它为我工作

我把我的答案包括在内,这样我下次就可以参考了。

我用它来转储一个存档:

mongodb——archive——db=mongodb+srv://$HOST/$OLD_DB > backup . zip

这将恢复到一个新的db:

mongorestore——nsInclude = " OLD_DB美元。*”——nsFrom = ' OLD_DB美元。*’——nsTo = ' NEW_DB美元。*' mongodb+srv://$HOST/$NEW_DB——archive=backup . //

注意nsInclude, nsFrom和nsTo参数。

这是我的mongorestore和mongodump版本

mongodump --version
mongodump version: 100.6.0
git version: 1d46e6e7021f2f5668763dba624e34bb39208cb0
Go version: go1.17.10
   os: darwin
   arch: amd64
   compiler: gc

mongorestore --version
mongorestore version: 100.6.0
git version: 1d46e6e7021f2f5668763dba624e34bb39208cb0
Go version: go1.17.10
   os: darwin
   arch: amd64
   compiler: gc

谁知道这是否适用于不同的版本。