query data via mongos, when one shard in MongoDB Sharded cluster is not available?

Hi community!
I want to query data via mongos, when one shard in MongoDB Sharded cluster is not available, it should be possible according to docs

[url]https://docs.mongodb.com/…/…/troubleshoot-sharded-clusters/…[/url]

“However, the data on all other shards will remain available, and it is possible to read and write data to the other shards. However, your application must be able to deal with partial results, and you should investigate the cause of the interruption and attempt to recover the shard as soon as possible.”

But in my case i always get “errmsg” : “None of the hosts for replica set XXX could be contacted.”, despite the fact i actually asked data, which are located in “live” shard (replica set).

Any ideas ?
Mongodb version - 3.2.17

Hi,

Yes you are right. You could fetch data from the cluster even a shard is down. But it is possible only when mongos could target those data from the live shards through shard keys. That means, the query should include the shard key. If not, mongos will broadcast the query request to all shard and receives error when one is not up.

You can read more about this target vs broadcast requests here - https://docs.mongodb.com/manual/core/sharded-cluster-query-router/index.html#targeted-operations-vs-broadcast-operations

For example: Insert some value in mongos and here field “id” is shard key.

mongos> db.testData.insert({id: 100001, name: "Alex"})
WriteResult({ "nInserted" : 1 })
mongos> db.testData.insert({id: 100002, name: "Julie"})
WriteResult({ "nInserted" : 1 })

Now shutdown one shard:

[root@vinodh-krishnaswamy ~]# mlaunch stop shard03
sent signal 15 to 3 processes.

Now check the data again from mongos with or without shard key and check the results:

mongos> db.testData.find({id:100001})
{ "_id" : ObjectId("5c712e53831dfa8c69cf274d"), "id" : 100001, "name" : "Alex" }
mongos> db.testData.find({name:"Alex"})
error: {
"$err" : "ReplicaSetMonitor no master found for set: shard03",
"code" : 10009,
"shard" : "shard03"
}

Hope this helps you.