Skip to content

Instantly share code, notes, and snippets.

@hwatkins
Created September 18, 2012 18:57
Show Gist options
  • Save hwatkins/3745068 to your computer and use it in GitHub Desktop.
Save hwatkins/3745068 to your computer and use it in GitHub Desktop.
Mongoid 3.x multiple database
If you want to use multiple dbs at once there are several different ways...
1) If you want to do this on a per-model level, use .store_in (This is for all threads):
class Band
include Mongoid::Document
store_in database: "secondary" # This can be any name you want, no need to put it in the mongoid.yml.
end
class Artist
include Mongoid::Document
store_in database: "tertiary"
end
2) If you want to do this on a per-operation level, use #with (This is for current thread only):
Band.with(database: "secondary").create(...)!
band.with(database: "tertiary").update_attributes(...)
3) If you want to override this on a global level, like per-request switching, use .override_database (This is for current thread only):
Mongoid.override_database("secondary")
Band.create # Persists to secondary
band.save # Persists to secondary
I am assuming you mean in this case multiple databases in the same server or replica set...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment