Testing Azure MongoDB connectivity with Python

Ovidiu Borlean
1 min readNov 26, 2022

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
  • Python3-pip
  • pymongo (PIP Package)
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