With ismaster now deprecated in the latest driver what is the best way to check server availability using the new hello command and pymongo.
In the old documentation it was recommended to use client.admin.command(“ismaster”) for availability, what would be the correct way to it so that it would do the correct command for each mongo version?
I was reading through the source code and could find that you can get the right command for the mongo version with sock_info.hello_cmd(). Is the best to do it somehow using hello_cmd method?
This is old instruction is still in the latest source code as comment: mongo-python-driver/mongo_client.py at c93194a2e6a5a4e3caada9e7e288cb2ab373f4cd · mongodb/mongo-python-driver · GitHub
You can check if the server is available
from pymongo.errors import ConnectionFailure client = MongoClient() try: # The ismaster command is cheap and does not require auth. client.admin.command('ismaster') except ConnectionFailure: print("Server not available")
From the 3.12 change log:
- Deprecated [
IsMaster]which will be removed in PyMongo 4.0 and are replaced by [
hellowhich provide the same API.