2

we have MongoDB 2.6 and 2 Replica Set, and we use pymongo driver and connect Mongo Replicat Set with the following url

mongodb://admin:admin@127.0.0.1:10011:127.0.0.1:10012,127.0.0.1:10013/db?replicaSet=replica

with python code

from pymongo import MongoClient
url = 'mongodb://admin:admin@127.0.0.1:10011:127.0.0.1:10012,127.0.0.1:10013/db?replicaSet=replica'
db = 'db'
db = MongoClient(
    url,
    readPreference='secondary',
    secondary_acceptable_latency_ms=1000,
)[db]
db.test.find_one()
# more read operations

but it turns out that the connection didn't read anything from secondary replicat set, no connection log could be found in mongo log on these 2 secondary replica set

GCooper
  • 140
  • 6

1 Answers1

0

Try to configure a ReplicaSetConnection instead. For instance:

>>> db = ReplicaSetConnection("morton.local:27017", replicaSet='foo').test
>>> from pymongo import ReadPreference
>>> db.read_preference = ReadPreference.SECONDARY
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 2
    Connection and RepliaSetConnection are pretty old now. They were deprecated as they used unsafe writes by default. MongoClient and MongoReplicasetClient replaced them in 2.4 and set a write concern of 1 by default to ensure that unsuccessful writes are caught. Since 3.0 that has been deprecated in favour of just using MongoClient. http://api.mongodb.org/python/current/changelog.html – Mark Unsworth Jun 08 '15 at 10:23