How to read directly from mongo slave using mongodb in nodejs
<p>I am trying to read from a slave node but I get the following error.</p>

<pre><code>MongoError: not master
    at Function.MongoError.create (node_modules/mongodb-core/lib/error.js:31:11)
    at node_modules/mongodb-core/lib/topologies/server.js:777:66
    at Callbacks.emit (node_modules/mongodb-core/lib/topologies/server.js:95:3)
    at null.messageHandler (node_modules/mongodb-core/lib/topologies/server.js:249:23)
    at Socket.&lt;anonymous&gt; (node_modules/mongodb-core/lib/connection/connection.js:265:22)
    at emitOne (events.js:78:13)
    at Socket.emit (events.js:170:7)
    at readableAddChunk (_stream_readable.js:147:16)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:524:20)
</code></pre>

<p>Here is my code</p>

<pre><code>var MongoClient = require('mongodb').MongoClient
MongoClient.connect('mongodb://slave.example.com/?slaveOk=true', {slaveOk: true}, (err, db) =&gt; {
   if (err) { throw err }
   d = db.db('order', {server: {slaveOk: true}})
   d.eval('rs.slaveOk(); 1', (err, data) =&gt; {
     if (err) { throw err }
     console.log(data);
   })
})
</code></pre>

<p>I am providing the complete list of hosts in the connection string because the node that is running this query only has access to the secondary MongoDB nodes.</p>

<p>When using the command line interface I am able to make queries as long as I run <code>rs.slaveOk()</code> before executing my query.</p>