Last Updated: February 25, 2016
·
2.155K
· dsci

Renaming a MongoDb collection

Recently I accidentally had a typo in my Mongoid Rails model where I specified a collection name to store the documents in.

After playing around with my app I discovered my mistake - and having existing associations by now - I wanted to fix this. So I removed the typo from the code and at the end I had to rename the collection within MongoDB.

Let's say the wrong collection name was wordifycsmexternal_links and the correct name wordifycmsexternal_links. I fired up my Mongo shell, select the database and renamed the collection:

db.wordify_csm_external_links.renameCollection('wordify_cms_external_links')