Skip to content

Instantly share code, notes, and snippets.

@fabioyamate
Created February 26, 2013 20:36
Show Gist options
  • Save fabioyamate/5041934 to your computer and use it in GitHub Desktop.
Save fabioyamate/5041934 to your computer and use it in GitHub Desktop.
MongoDB Moped rename collection
# Since Mongoid switched to its own driver, many of the mongo API
# (available in mongo-ruby-drive) are missing. This is allows you
# to rename a collection thru command message.
#
# Be aware that the API constantly changes in mongoid
#
# Mongoid (3.1.0), Moped (1.4.2)
class MyModel
include Mongoid::Document
def self.rename(to, dropTarget = false)
database = collection.database
database.session.with(:database => "admin", :consistency => :strong) do |session|
session.command({ :renameCollection => "#{database.name}.#{collection_name}", :to => "#{database.name}.#{to}", :dropTarget => dropTarget })
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment