How do you rename a MongoDB database?
我的MongoDB数据库名称中有一个拼写错误,我正在寻找重命名数据库。
我可以这样复制和删除......
1 2 3 | db.copyDatabase('old_name', 'new_name'); use old_name db.dropDatabase(); |
是否有重命名数据库的命令?
你可以这样做:
1 2 3 | db.copyDatabase("db_to_rename","db_renamed","localhost") use db_to_rename 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...
替代解决方案:您可以转储数据库并以不同的名称还原它。正如我所经历的那样,它比
1 2 | $ mongodump -d old_db_name -o mongodump/ $ mongorestore -d new_db_name mongodump/old_db_name |
http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/
警告:如果您不想在单个数据库下搞乱不同的集合,请在复制数据库时小心。
以下显示如何重命名
1 2 3 4 | > show dbs; testing games movies |
要重命名,请使用以下语法
1 | db.copyDatabase("old db name","new db name") |
例:
1 | db.copyDatabase('testing','newTesting') |
现在,您可以通过以下方式安全地删除旧数据库
1 2 3 | use testing; db.dropDatabase(); //Here the db **testing** is deleted successfully |
现在只考虑如果尝试使用现有数据库名称重命名新数据库名称会发生??什么
例:
1 | db.copyDatabase('testing','movies'); |
因此,在此上下文中,所有测试集合(表格)都将复制到电影数据库中。
上面的过程很慢,您可以使用下面的方法但是您需要将集合按集合移动到另一个db。
1 2 | use admin db.runCommand({renameCollection:"[db_old_name].[collection_name]", to:"[db_new_name].[collection_name]"}) |
虽然Mongodb不提供重命名数据库命令,但它提供了重命名Collection命令,该命令不仅修改了集合名称,还修改了数据库名称。
1 | db.adminCommand({renameCollection:"db1.test1", to:"db2.test2"}) |
这个命令只修改元数据,成本非常小,我们只需遍历
你可以在这个Js脚本中完成它
1 2 3 4 5 6 7 8 | 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}); } |
如果您将所有数据都放在管理数据库中(您不应该),您会注意到
1 2 3 4 5 6 | use old_db db.getCollectionNames().forEach(function(collName) { db[collName].find().forEach(function(d){ db.getSiblingDB('new_db')[collName].insert(d); }) }); |