4

I'm using MongoDB and MongoID in a rails app, how can some models be a part of a different mongo database on the same server? How would I accomplish something like that? I've run into the same problem with mysql before and couldn't find a reasonable solution.

Any thoughts?

JP Silvashy
  • 46,977
  • 48
  • 149
  • 227

1 Answers1

9

The newest versions of Mongoid support this. See the docs.

Snippets:

config/mongoid.yml:

defaults: &defaults
  host: localhost
  slaves:
    - host: localhost
      port: 27018
    - host: localhost
      port: 27019
  databases:
    secondary:
      database: secondary_database
      host: localhost
      port: 27020
      slaves:
        - host: localhost
          port: 27021
        - host: localhost
          port: 27022

In your model:

class Business
  include Mongoid::Document
  set_database :secondary
end
PreciousBodilyFluids
  • 11,881
  • 3
  • 37
  • 44
  • 1
    Config file not applicable to "newest" version of Mongoid anymore. Also link to docs has changed. – David Backeus Jan 13 '16 at 07:52
  • It looks like `secondary` config was removed. https://docs.mongodb.com/mongoid/current/tutorials/mongoid-configuration/ – B Seven Sep 22 '19 at 18:00