Connecting to a replica set
Connecting to a replica set is not fundamentally different from connecting to a single server. In this section, we will show some examples that use the official mongo-ruby-driver
. We will use the following steps for the replica set:
- First, we need to set our
host
andoptions
objects as follows:client_host = ['hostname:port'] client_options = { database: 'signals', replica_set: 'xmr_btc' }
In the preceding example, we are getting ready to connect to hostname:port
in the database signals in replica_set xmr_btc
.
- Calling the initializer on
Mongo::Client
will now return aclient
object that contains a connection to our replica set and database, as follows:client = Mongo::Client.new(client_host, client_options)
The client
object has the same options it has when connecting to a single server.
Note
MongoDB uses auto-discovery after connecting to our client_host
to identify the other...