@@ -2,8 +2,8 @@ Connecting to a Replica Set
2
2
===========================
3
3
4
4
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
7
7
connections with PyMongo.
8
8
9
9
.. note :: Replica sets require server version **>= 1.6.0**. Support
@@ -16,7 +16,7 @@ Starting a Replica Set
16
16
----------------------
17
17
18
18
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
20
20
about setting up a new replica set or migrating an existing MongoDB
21
21
setup, be sure to check that out. Here, we'll just do the bare minimum
22
22
to get a three node replica set setup locally.
@@ -110,7 +110,7 @@ can't happen completely transparently, however. Here we'll perform an
110
110
example failover to illustrate how everything behaves. First, we'll
111
111
connect to the replica set and perform a couple of basic operations::
112
112
113
- >>> db = Connection("morton.local").test
113
+ >>> db = Connection("morton.local", replicaSet='foo' ).test
114
114
>>> db.test.save({"x": 1})
115
115
ObjectId('...')
116
116
>>> db.test.find_one()
@@ -152,4 +152,34 @@ the operation will succeed::
152
152
>>> db.connection.port
153
153
27018
154
154
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.
155
185
0 commit comments