Search code examples
mongodbreplicationpymongosharding

In PyMongo, given a mongos, what is the difference between normal and RS clients?


PyMongo has a MongoClient for connecting to single nodes, and a MongoReplicaSetClient for connecting to entire replica sets; the latter is able to route reads to secondaries and monitor set health. But what is the difference if I connect to a mongos instead of the replica set nodes? As far as I understand, mongos handles all the routing and monitoring itself.


Solution

  • MongoClient is for connecting to standalones and mongos, because the driver doesn't have to handle failover in those cases. The mongos handles the routing, etc. when it is connected to replica sets. When connecting to a single replica set, use the MongoReplicaSetClient so the driver will handle autodiscovery of set members, failing over to a new primary, etc. See High Availability and PyMongo.