Is it better to do
from pymongo import Connection conn = Connection() db = conn.db_name def contrivedExample(firstName) global db return db.people.find_one({'first-name': firstName})
or
from pymongo import Connection def contrivedExample(firstName): with Connection() as conn: return conn.db_name.people.find_one({'first-name': firstName})
Various MongoDB basic tutorials (whether Python-oriented or not) imply that the application must connect once at startup; is this really the case? Does the response to non-trivial long-term applications change? Is the answer modified for web applications? What are the advantages / disadvantages of switching to a single connection or connection per request?
Assuming that โonce at startupโ is the correct answer, would it be wise to run this connection in __init__.py ?
source share