Python 如何在MongoDB(MongoEngine)中执行此简单查询?
如何从id=4EF1FDDB33C45091D000000节中id=1的主题获取所有消息 我有一些模型:Python 如何在MongoDB(MongoEngine)中执行此简单查询?,python,mongodb,mongoengine,Python,Mongodb,Mongoengine,如何从id=4EF1FDDB33C45091D000000节中id=1的主题获取所有消息 我有一些模型: class Message(EmbeddedDocument): id = SequenceField(unique=True) content = StringField() create_date = DateTimeField() user = StringField() active = BooleanField() class Theme(
class Message(EmbeddedDocument):
id = SequenceField(unique=True)
content = StringField()
create_date = DateTimeField()
user = StringField()
active = BooleanField()
class Theme(EmbeddedDocument):
id = SequenceField(unique=True)
title = StringField()
content = StringField()
create_date = DateTimeField()
user = StringField()
messages = ListField(EmbeddedDocumentField(Message))
active = BooleanField()
class Section(Document):
title = StringField(unique=True)
description = StringField()
themes = ListField(EmbeddedDocumentField(Theme))
该模型生成一些JSON,如下所示:
{
"_cls": "Section",
"_id": {
"$oid": "4ef1fddbb33c45091d000000"
},
"_types": [
"Section"
],
"description": "Test description",
"themes": [
{
"_types": [
"Theme"
],
"title": "Test",
"messages": [
{
"content": "I'm content!",
"_types": [
"Message"
],
"id": 12,
"_cls": "Message"
},
{
"content": "I'm second message!",
"_types": [
"Message"
],
"_cls": "Message",
"user": "inlanger",
"id": 13
}
],
"content": "Test description",
"_cls": "Theme",
"id": 1
},
{
"_types": [
"Theme"
],
"title": "Test2",
"messages": [
{
"_types": [
"Message"
],
"create_date": {
"$date": "2012-01-31T11:29:17.120Z"
},
"content": "Some message",
"_cls": "Message",
"id": 14,
"user": "inlanger"
}
],
"content": "Test description 2",
"_cls": "Theme",
"id": 2
},
{
"_types": [
"Theme"
],
"create_date": {
"$date": "2012-01-31T12:00:50.889Z"
},
"title": "Ататата",
"messages": [],
"content": "Theme number 3",
"user": "inlanger",
"id": 15,
"_cls": "Theme"
}
],
"title": "Test"
}
我使用一些代码。。。这很有效,但很难看:
def get_theme_messages(section_id, theme_id, page = 1):
section = Section.objects(id = section_id, themes__id = theme_id).first()
for theme in section.themes:
if theme.id == int(theme_id):
return theme.messages
break
else:
pass
MongoDB总是返回完整的文档(用Mongoengine的说法,就是
Document
实例)。如果要在文档
中的列表中过滤嵌入文档
的列表,则必须在客户端代码中进行过滤(如此处所示)
您可以通过删除一些不必要的行来稍微清理此代码:
def get_theme_messages(section_id, theme_id, page = 1):
section = Section.objects(id = section_id, themes__id = theme_id).first()
for theme in section.themes:
if theme.id == int(theme_id):
return theme.messages
(这在功能上等同于您在上面粘贴的内容)MongoDB始终返回完整文档(即,用Mongoengine的说法是
Document
实例)。如果要在文档
中的列表中过滤嵌入文档
的列表,则必须在客户端代码中进行过滤(如此处所示)
您可以通过删除一些不必要的行来稍微清理此代码:
def get_theme_messages(section_id, theme_id, page = 1):
section = Section.objects(id = section_id, themes__id = theme_id).first()
for theme in section.themes:
if theme.id == int(theme_id):
return theme.messages
(这在功能上等同于您在上面粘贴的内容)+1您可以限制从文档返回的顶级字段(即仅检索消息),但在这种情况下,这没有多大帮助。编辑:没关系,我知道这不是顶级字段。如果我有数千条记录,那么获取所有记录是正常的?如果你有数千条
嵌入文档,是的。如果返回的顶级文档
比最终需要的多,则始终可以筛选查询。+1您可以限制从文档返回的顶级字段(即仅检索消息),但在这种情况下,这没有多大帮助。编辑:没关系,我知道这不是顶级字段。如果我有数千条记录,那么获取所有记录是正常的?如果你有数千条嵌入文档,是的。如果返回的顶级文档比最终需要的多,则始终可以筛选查询。