mongoengine query a list of embedded documents
mongodb, mongoengine
Solution
As the comments are contained with the document then comments will always contain all comments.
Add a property to Post that filters and only returns a list of approved comments eg:
@property
def approved_comments(self):
return [comment for comment in self.comments if comment.approved]
Problem
I'm running into a classic pitfall, but can't find a good example with mongoengine of what I should be doing. Using the standard blog example I have something like: ``` class Comment(EmbeddedDocument): author = StringField() approved = BooleanField(default=False) class Post(Document): id = StringField(required=True, unique=True) comments = ListField(EmbeddedDocumentField(Comment)) ``` For a given blog post (with id `some_id`) I just want to load the list of approved comments. I keep accidentally loading all comments if any of the comments for the post are approved, because I'm matching an element of the list.