mongoidmongoid3

How do I rename a mongo collection in Mongoid?


I have a collection called artists, i'd like to rename it to artist_lookups. How do I do this?


Solution

  • From the Mongoid Docs:

    class Band
      include Mongoid::Document
      store_in collection: "artists", database: "music", session: "secondary"
    end
    

    Use store_in collection: "artist_lookups" in your model. This will let you store your Artist model in the artist_lookups collection.

    If you want to preserve the existing data in the artists collection, and rename it, I suggest shutting down your app temporarily, renaming the collection to artist_lookups on your MongoDB server, and then restarting the app.