Use the same connection for GetMore operation in PyMongo

Is it possible to use the same connection to obtain next batch (GetMore operation) in PyMongo? Here get_socket is called and new connection is returned to obtain next batch. It causes CursorNotFound error because we use balancer over multiple mongos instances. The problem is described here: mongodb - Load Balancing Between Mongos - Stack Overflow

Hi @Vladimir_Vladimirov welcome to the community.

Most official drivers (including pymongo) use a connection pool to manage their connection to the database (either mongod or mongos), and they are transparently managing the connections with getmores or any other network operations. Thus I’m not aware of any user-facing method to provide a low level instruction to pymongo.

As mentioned in one of the StackOverflow answers, pymongo was not designed to work with a custom load balancer in front of a number of mongos processes. I concur with the answerer there that what I think happened is that you executed a query from one mongos, then called getmore from another mongos due to the load balancer.

So the short answer is, no you cannot put a custom load balancer between pymongo and mongos, as they were designed to connect directly to each other.

Instead, pymongo provides a feature to connect to multiple mongos and do basic load balancing between them. See mongos Load Balancing for details and examples.

Best regards,
Kevin

2 Likes

Thank you for answer

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.