Although the pymongo client is extremely easy to use, it is much more convenient to wrap its functionality into a Connection class. We start by importing the MongoClient class. We then define two properties: one to represent the database and another to represent the client. Most importantly, take note of the third argument when creating a MongoClient instance – the name of the entity class associated with this connection:
# db.mongodb.connection
from pymongo import MongoClient
class Connection :
db = None
client = None
def __init__(self, host = 'localhost', port = 27017, \
result_class = dict) :
self.client = MongoClient(host, port, result_class)
Next, we define a number of useful methods to grant outside access to the client and database connection instances:
def getClient(self) :
return self.client
def getDatabase(self, database) :
self.db = self.client[database]
return self...