Hey @Omar_Dawah, welcome and thanks for the question!
What you’re describing actually sounds like normal behavior. Additionally, the code you provided seems correct and shouldn’t cause a connection leak.
MongoDB drivers open a minimum of 2 monitoring connections to each node in a MongoDB database, plus at least 1 connection to each node for user operations. For example, if you’re connecting to a 3-node replica set, MongoDB drivers will open 6 total monitoring connections before you run any operations. Monitoring connections are used to track the state of the database to maximize availability during topology changes or unexpected disconnects.
Even though your description doesn’t sound like a connection leak, I have a few questions to help me understand your situation:
- What version of the Go driver are you using?
- What MongoDB topology are you connecting to? For example, is it a standalone node, a replica set, a sharded cluster, Atlas Serverless, or something else?
- Are you connecting to a self-hosted or Atlas-hosted database?
Thanks!