Hi Gaurav
I tried Mongo::Client.new([SERVER_ADDRESS], { database: DB_NAME, connection: :direct })
I was able to connect to a primary directly using:
client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test', :connect => :direct)
In the console, this shows:
D, [2017-06-01T15:04:05.972907 #63409] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2017-06-01T15:04:05.973043 #63409] DEBUG -- : MONGODB | Server 127.0.0.1:27017 initializing.
D, [2017-06-01T15:04:05.975230 #63409] DEBUG -- : MONGODB | Server description for 127.0.0.1:27017 changed from 'unknown' to 'primary'.
Note the line Topology type 'single'
. Without the direct connection option, e.g.:
client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
this shows:
D, [2017-06-01T15:04:37.617541 #63417] DEBUG -- : MONGODB | Topology type 'unknown' initializing.
D, [2017-06-01T15:04:37.617698 #63417] DEBUG -- : MONGODB | Server 127.0.0.1:27017 initializing.
D, [2017-06-01T15:04:37.620555 #63417] DEBUG -- : MONGODB | Server description for 127.0.0.1:27017 changed from 'unknown' to 'primary'.
...
D, [2017-06-01T15:04:37.812831 #63417] DEBUG -- : MONGODB | Topology type 'unknown' changed to type 'replica set'.
Note the line Topology type 'unknown' changed to type 'replica set'.
above, which indicated that the driver discovered a replica set and acted accordingly. I tested the above code using the Ruby MongoDB driver version 2.4.2 and MongoDB 3.4.4. The connection code is the one available in the Using Mongo::Client section in the Ruby driver manual
If you’re still having issues, could you post:
mongod
logsBest regards,
Kevin