I have a Mongodb collection and I have created a Python class for documents in the collection. The class has some properties and methods which are not stored with the document. Should I try and store them to make the properties searchable or should I not store them and search the objects in Python?
Here is an example:
# Child
class Child:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@property
def parent(self):
try:
return Parent(**db.Parents.find_one({'child':self._id}))
except:
return None
# Parent
class Parent:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@property
def child(self):
try:
return Child(**db.Children.find_one({'parent':self._id}))
except:
return None
In this example, to search for all the children who’s parent’s name is “foo”, I have to do this:
results = [Child(**c) for c in db.Children.find() if c.parent.name == 'foo']
This means I have to pull all the Children documents from Mongodb and search them. Is it smarter to write the Parent data (or a subset of it) to the Child document, so I can use Mongodb to do the searching?? So my Child class could look like this:
# Child
class Child:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@property
def parent_name(self):
try:
return db.Parents.find_one({'child':self._id})['name']
except:
return None
def _save(self):
# something like this to get and save all the properties
data = {m[0]:getattr(self,m[0]) for m in inspect.getmembers(self)}
db.Children.find_and_modify({'_id':self._id},{'$set':data},upsert=True)
# search
results = [Child(**c) for c in db.Children.find({'parent_name':'foo'})]
So the search is more efficient, but I think having to keep the Child objects updated could be painful and dangerous. If I change the name of a Parent, I have to also rewrite its children. Feels wrong. Any better ideas???
You don’t have to load all
Children.(Also, why do you have both a
childfield on a parent and aparentfield on a child?)