python - 如何在python中使用mongoengine在ListField上添加索引?

标签 python mongodb mongoengine

我想在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/

相关文章:

python - 获取列表中出现频率最高的单词

python - 如何按另一个二维索引数组对二维 numpy 数组的每一行进行排序?

node.js - Mongoose:populate()/DBref 还是数据重复?

node.js - blogSchema 未定义

python - Django 休息总是创建新实例

Python 导入枚举错误

MongoDB Stitch 返回数据作为 $NumberDouble 而不是数字本身

mongodb - 快速写入 MongoDB 控制台,缓慢写入 ReplSet

python - Mongo 连接从未发布 - Django 和 Mongoengine 使用 gevent 在 gunicorn 上运行

Python:将 "\"更改为 "\\"