2

Mongoid provides several options to change which mongodb database is being used, either by specifying the new database or changing the session name to one that is already defined in the mongoid.yml file.

I was wondering if there is a way to change the session (ie mongodb host) while running the application.

So for example, if I started my application and it initially works off the main database on server 1, is there a way to say use the sample database on server 2

2 Answers 2

3

The following allows users to add sessions dynamically

session_hash = {"database" => "testmongo", "hosts" => ["127.0.0.1:3003"], "username" => "testuser", "password" => "test_password"}
Mongoid::Config.sessions[:mongo_dynamic] = session_hash
Sign up to request clarification or add additional context in comments.

1 Comment

Can you tell me what will be the next step to get/set data from/to the different database?
2
Model.with(database: "other_db_name").create()

I don't know if you can specify a different server. But its worth a try

1 Comment

This doesn't work because it assumes that the database is defined in the mongoid.yml file, whereas the connection needs to be created dynamically, for example if I'm pulling the connection parameters from a database and need to add it to the mongoid session collection.

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.