stackoverflow question
mongo doc read preference
Connecting to a Replica Set
The initial connection as made above is a special case for an uninitialized replica set. Normally we’ll want to connect differently. A connection to a replica set can be made using the MongoClient()
constructor, specifying one or more members of the set, along with the replica set name. Any of the following connects to the replica set we just created:
>>> MongoClient('localhost', replicaset='foo')
MongoClient('localhost', 27017)
>>> MongoClient('localhost:27018', replicaset='foo')
MongoClient('localhost', 27018)
>>> MongoClient('localhost', 27019, replicaset='foo')
MongoClient('localhost', 27019)
>>> MongoClient('mongodb://localhost:27017,localhost:27018/?replicaSet=foo')
MongoClient(['localhost:27017', 'localhost:27018'])
The addresses passed to MongoClient()
are called the seeds. As long as at least one of the seeds is online, MongoClient discovers all the members in the replica set, and determines which is the current primary and which are secondaries or arbiters.
Secondary Reads
By default an instance of MongoClient sends queries to the primary member of the replica set. To use secondaries for queries we have to change the read preference:
>>> client = MongoClient(... 'localhost:27017',... replicaSet='foo',... readPreference='secondaryPreferred')
>>> client.read_preferenceSecondaryPreferred(tag_sets=None)
Now all queries will be sent to the secondary members of the set. If there are no secondary members the primary will be used as a fallback. If you have queries you would prefer to never send to the primary you can specify that using the secondary
read preference.
By default the read preference of a Database
is inherited from its MongoClient, and the read preference of a Collection
is inherited from its Database. To use a different read preference use the get_database()
method, or the get_collection()
method:
>>> from pymongo import ReadPreference
>>> client.read_preferenceSecondaryPreferred(tag_sets=None)
>>> db =client.get_database('test',read_preference=ReadPreference.SECONDARY)
>>> db.read_preferenceSecondary(tag_sets=None)
>>> coll = db.get_collection('test', read_preference=ReadPreference.PRIMARY)
>>> coll.read_preferencePrimary()
You can also change the read preference of an existing Collection
with the with_options()
method:
>>> coll2 = coll.with_options(read_preference=ReadPreference.NEAREST)
>>> coll.read_preferencePrimary()
>>> coll2.read_preferenceNearest(tag_sets=None)
Note that since most database commands can only be sent to the primary of a replica set, the command()
method does not obey the Database’s read_preference
, but you can pass an explicit read preference to the method:
>>> db.command('dbstats', read_preference=ReadPreference.NEAREST){...}
Reads are configured using three options: read preference, tag sets, and local threshold.
Read preference:
Read preference is configured using one of the classes from read_preferences
(Primary
, PrimaryPreferred
, Secondary
, SecondaryPreferred
, or Nearest
). For convenience, we also provide ReadPreference
with the following attributes:
PRIMARY
: Read from the primary. This is the default read preference, and provides the strongest consistency. If no primary is available, raise AutoReconnect
.
PRIMARY_PREFERRED
: Read from the primary if available, otherwise read from a secondary.
SECONDARY
: Read from a secondary. If no matching secondary is available, raise AutoReconnect
.
SECONDARY_PREFERRED
: Read from a secondary if available, otherwise from the primary.
NEAREST
: Read from any available member.