I am trying to define methods for performing checks and updates to a listfield of embedded documents in mongoengine. What is the proper way of doing what I'm trying to do. The code is below.
class Comment(EmbeddedDocument):
created = DateTimeField()
text = StringField()
class Post(Document):
comments = ListField(EmbeddedDocumentField(Comment))
def check_comment(self, comment):
for existing_comment in self.comments:
if comment.created == existing_comment.created and
comment.text == existing_comment.text:
return True
return False
def add_or_replace_comment(self, comment):
for existing_comment in self.comments:
if comment.created == existing_comment.created:
# how do I replace?
# how do I add?
Is this even the correct way to go about something like this?