mongodb - Mongoengine: How to append a new document to an Embedded ListField document? -
mongodb - Mongoengine: How to append a new document to an Embedded ListField document? -
i append new listfield embeddeddocument existing listfield embeddeddocument document. in other words appending new document list belongs a document in list.
my model: post can contain several comments, each comment can have several likes:
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: (it's not working 'append' command dosen't exists, 'set' exists)
def appendnewlike(): user = {..} target = objectid(commentid) newlike = like(user=user) product.objects(comments__comment_id=target).update(append_comments__s__likes=newlike)
ideal solution like:
def appendnewlike(): user = {..} target = objectid(commentid) newlike = like(user=user) product.objects(comments__comment_id=target).comments.likes.append(newlike)
comments? suggestions?
you want $push
new item list eg:
product.objects(comments__comment_id=target).update( push__comments__s__likes=newlike)
however, there bigger issues here. schema not ideal - ever growing arrays might cause issues document grows have moved on disk new extent (so can fit), if continually growing impact performance.
see data modeling docs more information.
mongodb append mongoengine listfield
Comments
Post a Comment