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

Diego
  • 1
  • 1

1 Answers1

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
Diego
  • 1
  • 1