我想在ListField上添加索引。这是我的代码:
class Post(Document):
meta = {"indexs":"testcomments.comment_id"}
_id = StringField()
txt = StringField()
testcomments = EmbeddedDocumentField(Comment)
comments = ListField(EmbeddedDocumentField(Comment))
class Comment(EmbeddedDocument):
comment = StringField()
comment_id = StringField()
...
...
我知道如何在EmbeddedDocumentField上添加索引(meta = {"indexs":"testcomments.comment_id"}
),但是如何在评论上添加索引?
最佳答案
我相信列表的工作方式相同,因此
meta = {
"indexes": [
"testcomments.comment_id",
"comments.comment_id", # or simply 'comments' if you want a multikey index
]
}
请注意,您可以检查正在创建的索引
col = Page._get_collection()
c.index_information()
如果您使用字典形式定义索引,例如:meta = {'indexes': [{'fields': ['comments.comment_id']}}
,您可以更精细地索引定义(以及更接近 pymongo/mongodb 的语法)
关于python - 如何在python中使用mongoengine在ListField上添加索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56070415/