1024programmer Java MongoEngine:Close connection

MongoEngine:Close connection

I spent years trying to find a simple example where MongoEngine was used and the connection was closed. Finally figured it out and posted my code.

1 > malla..:

I thought disconnect() should have been used originally, but it has been removed as a synonym for close().

from mongoengine import connect

 def main():

     #connect to db
     db_client = connect('my_db', host='localhost', port=27017)

     #close the connection

 if __name__ == "__main__":

2> Ash..:

I know this is an old question, but if anyone is searching, I thought I would give an alternative answer.

close() does not actually delete the connection from MongoEngine’s connection list. This can cause problems when trying to connect to other databases later.

To solve this problem, I used mongoengine.connection.disconnect (even though __all__ was not listed). My code looks like this:

from mongoengine import connect
 from mongoengine.connection import disconnect

 db = connect(alias='some_alias')

 {do stuff}


You can also omit the alias as it will default to “default” on connects and disconnects.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/764812

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top