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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Config file not applicable to "newest" version of Mongoid anymore. Also link to docs has changed.
It looks like secondary config was removed. docs.mongodb.com/mongoid/current/tutorials/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.