ITPub博客

首页 > 数据库 > NoSQL > MongoDB重命名database的方法一例

MongoDB重命名database的方法一例

原创 NoSQL 作者:chenfeng 时间:2020-02-11 17:11:01 0 删除 编辑

重命名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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2675149/,如需转载,请注明出处,否则将追究法律责任。

全部评论
交流MySQL,MongoDB和Redis技术。 微信或QQ:410294

注册时间:2015-12-07

  • 博文量
    750
  • 访问量
    1968501