i am trying to connect mongodb using pymongo 3.2. firstly, i followed the instruction here to setup a replica set. Then i went to my primary server and try "rs.status()", which returned:
{
"set" : "seekCluster",
"date" : ISODate("2015-12-22T04:44:12.411Z"),
"myState" : 1,
"term" : NumberLong(9),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "seek1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4677,
"optime" : {
"ts" : Timestamp(1450754784, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2015-12-22T03:26:24Z"),
"lastHeartbeat" : ISODate("2015-12-22T04:44:12.044Z"),
"lastHeartbeatRecv" : ISODate("2015-12-22T04:44:12.389Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "seek3:27017",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "seek2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4677,
"optime" : {
"ts" : Timestamp(1450754784, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2015-12-22T03:26:24Z"),
"lastHeartbeat" : ISODate("2015-12-22T04:44:12.170Z"),
"lastHeartbeatRecv" : ISODate("2015-12-22T04:44:10.833Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "seek3:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "seek3:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4679,
"optime" : {
"ts" : Timestamp(1450754784, 2),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2015-12-22T03:26:24Z"),
"electionTime" : Timestamp(1450754784, 1),
"electionDate" : ISODate("2015-12-22T03:26:24Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}Later in pymongo, i did:
from pymongo import ReadPreference
db = MongoClient("seek3", replicaSet='seekCluster',
readPreference='secondary',
localThresholdMS = 35)['seek']
print db.read_preference # it yields "Secondary(tag_sets=None)"
print db["db"].find().count(), db.clienthowever, by printing "db.client" that gave me a: ('seek3', 27017), which is my primary server.
my questions are: 1. is that correct or wrong? 2. how can i know whether the query returns the data from a secondary server?
...
con = MongoClient(hosts, event_listeners=[CommandLogger()], replicaSet='test', readPreference="secondary")...
...
...
Note
command()does not obeyread_preferenceorcodec_options. You must use the read_preference and codec_options parameters instead.
...
...