title: Pymongo : how to ensure connection and reading from only desired host (especially SECONDARY) in a replica set
body: <p><strong>Background</strong> </p>
  
  <p>mongoDB production cluster (not sharded) 3 hosts. They belong to replica set rs0 </p>
  
  <ul>
  <li>H1 - Primary</li>
  <li>H2 - Secondary</li>
  <li>H3 - Secondary</li>
  </ul>
  
  <p>I am trying to write a <strong><em>python</em></strong> script using <strong><em>pymongo</em></strong>. This being a production cluster, I want the script to read only from <strong><em>H3 secondary replica set</em></strong> so as to not burden primary and keep it free for its regular activities. The data will be fetched from a collection called "devices" in the <strong><em>batches of 2000 documents</em></strong>. This should further reduce read operations overhead.</p>
  
  <p><strong>Relevant Code</strong></p>
  
  <pre><code>client = MongoClient('mongodb://H3-hostname:27017/', replicaset='rs0', readPreference='secondary')
  .
  .
  .
  .
  .
  devices = devices_collection.find({"status" : "ACTIVE"},{"key" : 1, "username" : 1}, sort = [("key", pymongo.ASCENDING)]).batch_size(2000)
  </code></pre>
  
  <p>Also tried creating the client in a little different way by mentioning all the hosts in the cluster as below</p>
  
  <pre><code>client = MongoClient('mongodb://H1-Hostname:27017, H2-Hostname:27017, H3-hostname:27017/?replicaset=rs0', readPreference='secondary')
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>As per my understanding, the above client will read <strong><em>ONLY</em></strong> from the secondary replica set. But when I run the script and look at the cloud manager, H1 which is primary shows page faults increased from ~15-20 to ~600 with a sharp jump.</p>
  
  <p><strong>Environment</strong></p>
  
  <ul>
  <li>Mongo DB 3.0.3 (MMAPv1 storage engine)</li>
  <li>Python 2.7.9</li>
  <li>Pymongo 3.0.3</li>
  <li>OS Amazon Linux</li>
  </ul>