Testing Azure MongoDB connectivity with Python

The objective of this script is to test connectivity with a Azure MongoDB Instance and measure the latency of that operation in a loop.

The following packages needs to be installed in order to run this script:

  • Python3
apt update
apt install python3 -y
apt install python3-pip -y
pip3 install pymongo

As the library is using CosmosDB ConnectionString for initializing connection, we need to obtain this from Azure Portal and add into our script.

import pymongo
import time

def mongoTest():
st = time.time()
ConnStr = "Add here your Connection String"
client = pymongo.MongoClient(ConnStr)
client.server_info() # validate connection string
et = time.time()
elapsed_time = et - st
print("Connected in ", elapsed_time, "seconds" )
except (
) as err:
sys.exit("Can't connect:" + str(err))
except Exception as err:
sys.exit("Error:" + str(err))

if __name__ == '__main__':
print("Running MongoDB Testing script")
while True:

GitHub Repository: https://github.com/OvidiuBorlean/mongodbtest



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store