Skip to content

Commit 6b0f7db

Browse files
committed
Updated replica set examples doc.
1 parent 235424b commit 6b0f7db

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

doc/examples/replica_set.rst

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Connecting to a Replica Set
22
===========================
33

44
PyMongo makes working with `replica sets
5-
<http://dochub.mongodb.org/core/rs>`_ easy. Here we'll launch a new
6-
replica set and show how to handle both initialization and normal
5+
<http://www.mongodb.org/display/DOCS/Replica+Sets>`_ easy. Here we'll launch
6+
a new replica set and show how to handle both initialization and normal
77
connections with PyMongo.
88

99
.. note:: Replica sets require server version **>= 1.6.0**. Support
@@ -16,7 +16,7 @@ Starting a Replica Set
1616
----------------------
1717

1818
The main `replica set documentation
19-
<http://dochub.mongodb.org/core/rs>`_ contains extensive information
19+
<http://www.mongodb.org/display/DOCS/Replica+Sets>`_ contains extensive information
2020
about setting up a new replica set or migrating an existing MongoDB
2121
setup, be sure to check that out. Here, we'll just do the bare minimum
2222
to get a three node replica set setup locally.
@@ -110,7 +110,7 @@ can't happen completely transparently, however. Here we'll perform an
110110
example failover to illustrate how everything behaves. First, we'll
111111
connect to the replica set and perform a couple of basic operations::
112112

113-
>>> db = Connection("morton.local").test
113+
>>> db = Connection("morton.local", replicaSet='foo').test
114114
>>> db.test.save({"x": 1})
115115
ObjectId('...')
116116
>>> db.test.find_one()
@@ -152,4 +152,34 @@ the operation will succeed::
152152
>>> db.connection.port
153153
27018
154154

155+
ReplicaSetConnection
156+
--------------------
157+
158+
In Pymongo-2.1 a new ReplicaSetConnection class was added that provides
159+
some new features not supported in the original Connection class. The most
160+
important of these is the ability to distribute queries to the secondary
161+
members of a replica set. To connect using ReplicaSetConnection just
162+
provide a host:port pair and the name of the replica set::
163+
164+
>>> from pymongo import ReplicaSetConnection
165+
>>> ReplicaSetConnection("morton.local:27017", replicaSet='foo')
166+
ReplicaSetConnection([u'morton.local:27019', u'morton.local:27017', u'morton.local:27018'])
167+
168+
By default an instance of ReplicaSetConnection will only send queries to
169+
the primary member of the replica set. To use secondary members for queries
170+
we have to change the read preference::
171+
172+
>>> db = ReplicaSetConnection("morton.local:27017", replicaSet='foo').test
173+
>>> from pymongo import ReadPreference
174+
>>> db.read_preference = ReadPreference.SECONDARY
175+
176+
Now all queries will be sent to the secondary members of the set. If there are
177+
no secondary members the primary will be used as a fallback. If you have
178+
queries you would prefer to never send to the primary you can specify that
179+
using the SECONDARY_ONLY read preference::
180+
181+
>>> db.read_preference = ReadPreference.SECONDARY_ONLY
182+
183+
Read preference can be set on a connection, database, collection, or on a
184+
per-query basis.
155185

0 commit comments

Comments
 (0)