title: mongodb returning error using $out with mongos
body: <p>I have a mongodb cluster containing two shards, each a replication set of 3 nodes, and a config server cluster again with three nodes.</p>
  
  <p>All seems to be in order apart from when I attempt to write a collection at the end of an aggregation pipeline using the $out operator:</p>
  
  <pre><code>db.getCollection('restaurants').aggregate([
   {$match : {$text : {$search : "steak"}}},
   {$out : "steak_restaurants"}
  ])
  </code></pre>
  
  <p>This is return the following error:</p>
  
  <p>ailed to execute script.</p>
  
  <blockquote>
    <p>Error: Assert: command failed: {  "ok" : 0,   "errmsg" : "listIndexes
    failed: { ok: 0.0, errmsg: \"not master and slaveOk=false\", code:
    13435, codeName: \"NotMasterNoSlaveOk\" }",   "code" : 18631,
      "codeName" : "Location18631" } : aggregate failed
    _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:16:14
    assert.commandWorked@src/mongo/shell/assert.js:370:5
    DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
    @(shell):1:1</p>
    
    <p>Error: command failed: {  "ok" : 0,   "errmsg" : "listIndexes failed: {
    ok: 0.0, errmsg: \"not master and slaveOk=false\", code: 13435,
    codeName: \"NotMasterNoSlaveOk\" }",  "code" : 18631,     "codeName" :
    "Location18631" } : aggregate failed :
    _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:16:14
    assert.commandWorked@src/mongo/shell/assert.js:370:5
    DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
    @(shell):1:1</p>
  </blockquote>
  
  <p>The mongo shell is connected to a mongos router. If I execute the same against mongod, this works fine. </p>
  
  <p>The error suggests that somehow reading is taking place from a secondary, however, I have not set any read preference and I would have thought that mongos would resolve the primary correctly by itself. Any ideas or insight please?</p>