I am building an android app, with a Backend in python that uses MongoDB.
I am currently trying the handle the case where the ip is not added to MongoDB - meaning the server cant access it.
So I used try
and except
on all the MongoDB request, for example:
try:
matching_locations_list = list(location_collection.find({"username": username}))
except pymongo.errors.PyMongoError:
return ["Problem with connecting to MongoDB"]
But, it takes a bit of time, usually around 30 seconds, for MongoDB to throw the following error (which i catch):
pymongo.errors.ServerSelectionTimeoutError
My question is - Is there a way to set this timeout? So my app does not have to be waiting for around 30 second for the server to return "Problem with DB"
Tried using the following:
# Connection options
connection_options = {
"connectTimeoutMS": 5000, # Set the connection timeout value in milliseconds
"socketTimeoutMS": 5000, # Set the socket timeout value in milliseconds
}
and then:
client = pymongo.MongoClient(connection_string, **connection_options)
But it didn't same to make any difference.