【问题标题】:How I can do this simple query in MongoDB (MongoEngine)?我如何在 MongoDB (MongoEngine) 中进行这个简单的查询?
【发布时间】:2012-01-31 17:36:45
【问题描述】:

如何在 _id = 4ef1fddbb33c45091d000000 的部分中从 id = 1 的主题获取所有消息?

我有一些模型:

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

【问题讨论】:

    标签: python mongodb mongoengine


    【解决方案1】:

    MongoDB 总是返回完整的文档(即 Document 实例,用 Mongoengine 的说法)。如果您想在Document 中的列表中过滤EmbeddedDocuments 的列表,您必须在客户端代码中进行(如您在此处显示的那样)。

    您可以通过删除一些不必要的行来稍微清理一下这段代码:

    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 您可以限制从文档中返回的顶级字段(即仅检索消息),但这在这种情况下没有多大帮助。编辑:没关系,我看到那不是顶级字段。
    • 如果我有上千条记录 全部记录下来是正常的吗?
    • 如果您有数千个EmbeddedDocuments,是的。如果您返回的顶级Documents 比您最终需要的多,您始终可以过滤您的查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2020-05-23
    • 2015-09-15
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多