title: How to set slaveOk on MongoDB through nodejs driver?
body: <p>I have a mongo replica including one primary two secondaries. There are some accounts only have access to the secondary server. When I connect to one of the secondary through nodejs driver, I couldn't find a way to enable <code>slaveOk</code>. Below is my code. I have to connect to it as a normal server rather than a replica.</p>
  
  <pre><code>options = options || {
          connectTimeoutMS: 30000,
          socketTimeoutMS: 30000,
          // retry to connect for 30 times
          reconnectTries: 30,
          // wait 1 second before retrying
          reconnectInterval: 1000,
          readPreference: mongodb.ReadPreference.SECONDARY_PREFERRED
        };
  mongoClient.connect("mongodb://user:pwd@10.0.0.1/test", options, (err, db) =&gt; {
      db.command({listCollections: 1})
         .catch((e) =&gt; {
           // get exception here about "not master and slaveOk=false" 
         })
  }
  </code></pre>
  
  <p>when I run above code, I will get the exception saying that <code>not master and slaveOk=false</code>. I know that I need to enable <code>slaveOk</code> on the connection before run <code>db.command({listCollections: 1})</code> but I didn't find a way for that.
  I know there is a <code>readPreference</code> on options but I have tried below values none of them work:</p>
  
  <pre><code>ReadPreference.PRIMARY, 
  ReadPreference.PRIMARY_PREFERRED, 
  ReadPreference.SECONDARY, 
  ReadPreference.SECONDARY_PREFERRED, 
  ReadPreference.NEAREST
  </code></pre>
  
  <p>In Mongo Shell, I can run <code>rs.slaveOk()</code> command to enable read on the connection on secondary server. Is there a way to enable <code>slaveOk</code> on nodejs driver? So I can run <code>listCollection</code> command through dirver.</p>