0
我不知道是否好,如果我保持对数据库和集合的引用作为类成员?保持对数据库和集合的引用MongoDB python
就像那个
from pymongo import MongoClient
class ClientDataStore(object):
BASE_MONGO_CONNECTION_URL = 'mongodb://localhost:27017/'
MAIN_DB_NAME = "bank"
CLIENT_COLLECTION_NAME = "client"
def __init__(self):
self.mongo = MongoClient(ClientDataStore.BASE_MONGO_CONNECTION_URL)
self.db = self.mongo[ClientDataStore.MAIN_DB_NAME]
self.client_collection = self.db[ClientDataStore.CLIENT_COLLECTION_NAME]
def get_client_info(self, id):
client = self.client_collection.find_one({"_id": id})
return client
将它保持打开的连接或将在必要时打开它?
或者我应该打开数据库,并获得所有只有当我需要这个集合?
谢谢