Skip to content

Instantly share code, notes, and snippets.

@adamrunner
Created March 22, 2018 19:45
Show Gist options
  • Select an option

  • Save adamrunner/0214876f6afd1bdf9398589eb16947f5 to your computer and use it in GitHub Desktop.

Select an option

Save adamrunner/0214876f6afd1bdf9398589eb16947f5 to your computer and use it in GitHub Desktop.

Revisions

  1. adamrunner created this gist Mar 22, 2018.
    11 changes: 11 additions & 0 deletions rename_collection_mongoid.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    class Mongoid::Migration
    # renames a collection in mongodb, can be told to drop the target collection first
    def self.rename_collection(old_name, new_name, drop_target=false)
    client = Mongoid::Clients.default
    current_db = client.database
    admin_db = Mongo::Database.new(client, Mongo::Database::ADMIN, current_db.options)
    admin_db.command(renameCollection: "#{current_db.name}.#{old_name}",
    to: "#{current_db.name}.#{new_name}",
    dropTarget: drop_target)
    end
    end