How to close a mongodb python connection?
Use close()
method on your MongoClient
instance:
client = pymongo.MongoClient()# some code hereclient.close()
close()
is an alias for disconnect()
method:
Disconnecting will close all underlying sockets in the connection pool. If this instance is used again it will be automatically re-opened.
The safest way to close a pymongo connection would be to use it with 'with':
with pymongo.MongoClient(db_config['HOST']) as client: db = client[ db_config['NAME']] item = db["document"].find_one({'id':1}) print(item)