Mongodb – Accessing unsharded database in a MongoDB shard cluster without going through mongos

mongodb

As far as I can understand, not all databases in a MongoDB shard cluster have to be sharded; I can keep some databases unsharded, on only one of the shards.

So suppose I have a shard cluster, with shards (i.e. replica sets) rs0 and rs1. rs0 has a database foo, which is unsharded. I have no plan to shard it or move it to rs1.

Is there any harm in letting some clients connect to rs0 directly and use the foo database without going through mongos?

I have only tested that it is possible to connect directly to rs0 and make queries in foo, but I don't know if it is potentially dangerous in my use case.

Best Answer

Mongo will happily let you directly access any database on any individual node you hit. If that database is sharded, you can still access it but will only get results from that shard. This also means that you can have unsharded databases on a node and it'll return results from that unsharded database.

Yes, it is safe to directly access the replicaset members for that unsharded database.

It's also possible to do it through the MongoS process same as the sharded access. This may make code-paths easier, though it does add a bit of latency to the entire call.


  • Database is a feature of Nodes.
  • Sharding is a feature of Databases.
  • Replica status is a feature of Databases.
  • MongoS and Config servers represent the management layer needed to do Sharding.

A database that is not sharded does not need MongoS or Config, and can be directly accessed safely.