How to close a mongodb python connection?

connection, mongodb, pymongo, python

Solution

Use `close()` method on your `MongoClient` instance:

client = pymongo.MongoClient()

# some code here

client.close()

Cleanup client resources and disconnect from MongoDB.

End all server sessions created by this client by sending one or more endSessions commands.

Close all sockets in the connection pools and stop the monitor threads.

Problem

I'm doing a python script that writes some data to a mongodb. I need to close the connection and free some resources, when finishing. How is that done in Python?

Original source