在ListField内查询的最佳方法

问题描述 投票:0回答:1

我使用MongoEngine使用以下代码:

class Comment(EmbeddedDocument):
    content = StringField()
    name = StringField(max_length=120)


class Post(Document):
    title = StringField(max_length=120, required=True)
    author = StringField(required=True)
    tags = ListField(StringField(max_length=30))
    comments = ListField(EmbeddedDocumentField(Comment))

# Create a post:
post = Post(title="Quora rocks", author="Ross", tags=['tutorial', 'how-to'])
post.save()

comment1 = Comment(content="Great post!", name="john")
comment2 = Comment(content="Great post too!", name="dave")

post.comments.append(comment1)
post.comments.append(comment2)
post.save()

# Create a post:
post = Post(title="Books", author="Chandler")
post.save()

comment1 = Comment(content="Stupid post!", name="justin")
comment2 = Comment(content="Stupid post!", name="mark")

post.comments.append(comment1)
post.comments.append(comment2)
post.save()


我想先从Ross那里获取帖子,然后再从john找到评论。最好的方法是什么?我有MongoEngine 0.9.5

mongoengine
1个回答
0
投票

我想我已经解决了问题。我们需要EmbeddedDocumentListField而不是EmbeddedDocumentField的ListField。这使得过滤嵌入文档变得更加容易。

© www.soinside.com 2019 - 2024. All rights reserved.