Mongoengine: How to add a new document to an Embedded ListField document?

I would like to add a new ListField EmbeddedDocument to an existing ListField EmbeddedDocument. In other words, adding a new document to the list related to the document in the list.

My model: a message can contain several comments, each comment can have several comments:

class Post(Document): txt = StringField() comments = ListField(EmbeddedDocumentField(Comment)) class Comment(EmbeddedDocument): comment = StringField() comment_id = ObjectIdField() likes = ListField(EmbeddedDocumentField(Like)) class Like(EmbeddedDocument): user = ReferenceField(User) date = DateTimeField(default=datetime.utcnow,required=True) 

My Code: (the command 'append' dosen't does not exist, only 'set' exists)

 def appendNewLike(): user = {..} target = ObjectId(commentId) newLike = Like(user=user) Product.objects(comments__comment_id=target).update(append_comments__S__likes=newLike) 

An ideal solution would look something like this:

 def appendNewLike(): user = {..} target = ObjectId(commentId) newLike = Like(user=user) Product.objects(comments__comment_id=target).comments.likes.append(newLike) 

Comments? Suggestions?

+8
append mongodb mongoengine
source share
1 answer

You want $push add a new item to the list, for example:

 Product.objects(comments__comment_id=target).update( push__comments__S__likes=newLike) 

However, there are big problems. The scheme is not perfect - ever growing arrays can cause problems as the document grows, it will need to be moved to the disk to a new degree (so that it can fit) if it is constantly growing, which will affect performance.

See data modeling documents for more information .

+4
source share

All Articles