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, as follows:
- First, we need to set our host and options objects:
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 a client object that contains a connection to our replica set and database:
client = Mongo::Client.new(client_host, client_options)
The client object has the same options it has when connecting to a single server.
MongoDB uses auto-discovery after connecting...