I am bit stuck in MongoDB while exporting my output to some other collection.

Scenario :

Actually, i want to query out my result to another collection which is successful in normal mongoDB( without Sharding). But, when i Shard that same DB , Mongo DB is giving me error code 13435 which is cannot read from replica set and gives me below error set :

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 :

Currently working on below versions of mongoDB : Version : 3.4.6 OS : Windows Server 2012


The error message indicates that, in your cluster, there was shard what didn't have primary available and query went to secondary and with the current configuration, a query to secondary was not allowed.

So, what was a reason that there was no primary? An answer to that is the key to solve the problem.

Can you check (by login to every RS) that every sharded replica set have primary available? Second possibility is that config servers don't have up-to-date information of your shards configuration... But that should not be possible if your cluster works other ways fine.

  • Hi J Jussi, thanks for your prompt reply. But as per my case , i need to output my query results to a collection (i.e from sharded collection to any new collection). Is there any way we can achieve this? – yash vora Sep 1 '17 at 11:04
  • Oh, no problem was here.. Reading and understanding your question wrongly. I re-wrote my answer. – JJussi Sep 1 '17 at 13:44
  • Thanks J Jussi, for updating the answer. Now i got some real actual issue and as per your concerns in the post, the replica set have primary available but i dont know how to check for config servers. Would like if some help is done. Thanks and Cheers !!!! – yash vora Sep 4 '17 at 5:12
  • use mongo command (with --port 27019) to connect PRIMARY config server. Then rs.status(). – JJussi Sep 4 '17 at 9:56

Thanks a lot J Jussi, for giving me correct direction.

I ran rs.slaveOK(); command on each of the shards and then from windows cmd prompt ran my query containing $out parameter and it executed successfully giving me result in my target table.

If anybody is using $out parameter in there mongodb query to query out sharded collection result to another collection, please follow below steps:

  1. run rs.slaveOk(); by logging in each of primary shards
  2. Connect to mongos service.
  3. Execute your query containing $out parameter.
  4. Your result will be there.

Tested and verified.

  • rs.slaveOk() only works for the current connection when you are directly connected to a replica set member. This definitely has no effect on queries through mongos. Additionally, an aggregation pipeline with the $out stage has to run on primaries since it is writing data to a new output collection. I suspect your initial problem was due to some other user error; perhaps trying to run an aggregation pipeline with $out against a replica set secondary. If you have a sharded cluster you should always read and write via mongos for correct results. – Stennie Sep 4 '17 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.