I have the following class:
import sys
import os
import pymongo
from pymongo import MongoClient
class Collection():
client = MongoClient()
def __init__(self, db, collection_name):
self.db = db
self.collection_name = collection_name
def getCollection(self):
data_base = getattr(self.client, self.db)
collObject = getattr(data_base, self.collection_name)
return collObject
def getIdFromEmail(self, email):
collection = self.getCollection()
id = collection.find_one({"email":email},{"_id":1})
return id
When writing getIdFromEmail
it struck me that every time I want to get an id from an email, I'll be creating another collection object. Is there a way for the object to be created once as part of the class in stead of creating one every time I want to write a query?