3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MongoDBのDatabaseをリネームする

Posted at

MongoDBのDB名を変更する方法を紹介します。

残念ながら一発でアトミックにリネームする方法はないので、「コピーして古い方を削除」するしかありません。

ここでは「ダンプしてリストア」方式を紹介します。
具体的にはmongodumpmongorestoreというコマンドを使います。

old_dbという名前のデータベースをnew_dbに変更したいとします。

MongoDBサーバが稼働してる状態で、

$ mongodump old_db
$ mongorestore --db new_db ./dump/old_db

ここで、new_dbがちゃんと想定どおりの状態になっていることをテストます。
(具体的には、count()でレコード数を数えたり、アプリケーションから接続して動かしたり)

OKであれば旧DBを削除します。

$ use old_database
$ db.dropDatabase()
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?