MongoDB重新命名database的方法一例

chenfeng發表於2020-02-11

重新命名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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章