Shoken Startup Blog

KitchHike Founder/CTO

MongoDBでcollectionの名前を変更する方法

MongoDBでcollection名の変更は、db.fromColl.renameCollection(toColl)を使います。
dbshell Reference - MongoDB


MySLQで、テーブル名変更は以下のSQLに相当します。

ALTER TABLE 変更前テーブル名 RENAME TO 変更後テーブル名;

coll_testというcollection名をcoll_test2に変更する方法です。

[root@xxx ~]# mongo
MongoDB shell version: 2.0.4
connecting to: test
> use test
switched to db test
> show collections
coll_test
system.indexes
> db.coll_test.renameCollection('coll_test2')
{ "ok" : 1 }
> show collections
coll_test2         ## coll_test2に変更されています
system.indexes