Let's say we have the following.
class Post(Document):
uid = StringField(required=True, unique=True)
text = StringField(required=True
comments = EmbeddedDocumentListField(Comment)
class Comment(EmbeddedDocument):
comment_id = StringField(required=True)
comment = StringField(required=True)
datetime = DatetimeField()
So, we have already saved a Post without any comments. Each post is unique.
Then, I have a list of Comments objects. I want to do a for loop in order to save them one by one, or create a list of the comments objects and update once.
Also, I would like to check if some of these Comment objects are already in the Post.comment list field.
I have tried
for comment in comments:
o_comment = Comment()
o_comment.id = comment.get('id')
o_comment.comment = comment.get('comment')
o_comment.datetime = datetime.now()
Post.objects(uid = 'some_id').update_one(push__comments = o_comment)
So, this works but It appends the documents without checking. So if I run it many times I get duplicates.
Any idea ? Thanks again.