close() never close connections in pymongo?

2020-08-17 13:12发布

问题:

I use MongoDB and I connect to it through pymongo. Here's my code:

>>> import pymongo
>>> con=pymongo.Connection('localhost',27017)
>>> con.database_names()
['local', 'bookdb']
>>> con.close()
>>> con.database_names()
['local', 'bookdb']

I use con.close() to disconnect to the MongoDB, but after that, I can still use con.database_names() to see the list of the databases. Why? it never disconnect to the MongoDB server. Why the close() not work?

回答1:

Just read the docs, faster and more detailed.

If this instance is used again it will be automatically re-opened.

http://api.mongodb.org/python/current/api/pymongo/connection.html