如何在mongoengine EmbeddedDocument上创建索引?

2024-04-24 01:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有文档和嵌入文档。我想在EmbeddedDocument的字段上创建一个索引。 下面是示例结构和我尝试过的内容。你知道吗

from mongoengine import Document, EmbeddedDocument, fields


class Parent(Document):
    meta = {
        'collection': 'parent',
        'indexes': [
            'name',
            'child__name',  # throws mongoengine.errors.LookUpError: Cannot resolve field "child__name"
            'child.name',  # does nothing
        ],
    }
    name = fields.StringField()
    child = fields.EmbeddedDocumentField(Child)


class Child(EmbeddedDocument):
    meta = {
        'indexes': [
            'name',  # does nothing
        ],
    }
    name = fields.StringField()

这甚至可以创建这样的索引吗?你知道吗


Tags: name文档child示例fieldsdocumentmetaclass