MongoEngine: How to append document to ListField

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



MongoEngine: How to append document to ListField



I'm using mongodb with python.



Also use MongoEngine to communicate with mongodb.



Now I made some simple board system that has comment function.



[model.py]


import datetime
from mongoengine import *
from config import DB_NAME


connect(DB_NAME)


class User(Document):
no = SequenceField()
userid = StringField(unique=True, required=True)
userpw = StringField(required=True)
created_at = DateTimeField(default=datetime.datetime.now())


class Comment(EmbeddedDocument):
content = StringField(required=True)
writer = ReferenceField(User, required=True)


class Board(Document):
no = SequenceField()
subject = StringField(required=True)
content = StringField(required=True)
writer = ReferenceField(User, required=True)
comments = ListField(EmbeddedDocumentField(Comment))
created_at = DateTimeField(default=datetime.datetime.now())
updated_at = DateTimeField(default=datetime.datetime.now())



In this code, How can I append new list to Board's comments field?


Board


comments



After searching for a hour, some document says that,



Board.objects(no=_no).update_one(push__comments=['123', '456']) will be works perfectly.


Board.objects(no=_no).update_one(push__comments=['123', '456'])



But it throw mongoengine.errors.InvalidQueryError: Querying the embedded document 'Comment' failed, due to an invalid query value error.


mongoengine.errors.InvalidQueryError: Querying the embedded document 'Comment' failed, due to an invalid query value



Maybe there is some syntax error, But I'm new at MongoEngine.



How can I solve this issue?



Thanks.




1 Answer
1



[SOLVED]


comment = Comments(content='test', writer='hide')
board = Board.objects(no=_no).get()
board.comments.append(comment)
board.save()



I solved issue like this.



But, if is there any solution, please comment it.





yes thats the way to solve this
– Kartikeya Mishra
Aug 11 at 8:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard