Skip to content

Instantly share code, notes, and snippets.

@Unnumbered
Created March 17, 2017 09:40
Show Gist options
  • Save Unnumbered/2feac77bba9fd87a9b417021e6deb76e to your computer and use it in GitHub Desktop.
Save Unnumbered/2feac77bba9fd87a9b417021e6deb76e to your computer and use it in GitHub Desktop.
how to rename mongodb collection with mongoid 5.x
def 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment