0

I have a service in ruby and I trying to connect with two different hosts with two different databases

I'm trying something like this

mongoid.yml

development:
  clients:
    default:
      database: cpeTracking
      hosts:
       - development-shard.mongodb.net:27017
       - development-shard.mongodb.net:27017
       - development-shard.mongodb.net:27017
      options:
        user: my_user
        password: my_password
        auth_source: admin
        ssl: true
      database: testDb
        hosts:
        - localhost:27017

And my model

class Movies
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
  store_in database: "testDb"

  field :name, type: String
  field :year, type: Integer
  field :director, type: String
end

When I run the service, only connect with the first host. I also tried this solution Connecting to two databases Mongoid but didn't work

1
  • In your paste, all the hosts are the same though. Commented Mar 20, 2019 at 4:10

1 Answer 1

0

I found the answer. First I have to create a new client at same level of default, I called it "secondary" with the info of database and host.

development:
  clients:
    default:
      database: my_db
      hosts:
        - development-shard.mongodb.net:27017
        - development-shard.mongodb.net:27017
        - development-shard.mongodb.net:27017
      options:
        user: my_user
        password: my_pass
        auth_source: admin
        ssl: true
    secondary:
      database: testDb
      hosts:
       - localhost:27017

Second. In the model I had to add the collection, the database and the client that will use

class Movies
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
  store_in collection: "movies", database: "testDb", client: "secondary"

  field :name, type: String
  field :year, type: Integer
  field :director, type: String
end
Sign up to request clarification or add additional context in comments.

Comments

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.