MongoDB重命名database的方法一例

重命名database脚本如下:

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}); 

}

测试:

var source = “test”;

var dest = “test_new”;

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});

… }

{ “ok” : 1 }

use test_new

switched to db test_new

show tables

test

aaa

bbb

log

task

push_log

push_task

完整实例:http://github.crmeb.net/u/defu

来自 “开源世界 ” ,链接:http://ym.baisou.ltd/post/649.html,如需转载,请注明出处,否则将追究法律责任。

上一篇:内存操作函数


下一篇:实验一