mongoengine中的重复键错误索引

duplicate key error index in mongoengine

我有一个如下所示的 Page 模型,page_id 字段不是 unique,但我无法从 Page 创建实例,并且出现此错误
OperationError: Could not save document (insertDocument :: caused by ::11000 E11000 duplicate key error index: shopify.page.$pageId_1 dup key: { : null }) 我不知道为什么当 page_id 字段不是 unique 时重复键错误会上升

Site 模型:

class Page(Document):

    # page identity
    page_id = StringField()
    store = ReferenceField('Store')
    is_product = BooleanField(default = False)
    is_homepage = BooleanField(default = False)
    product = ReferenceField('Product')
    requests = ListField(EmbeddedDocumentField('Request'))

    # page stat
    visitors = IntField(default = 0)
    views = IntField(default = 0)
    past_days = ListField(EmbeddedDocumentField('DayStat'))

这只是因为数据库中的一些旧 Document 过去有不同的字段,并且当由于不同的对象类型而应用新更改时 Collection 当一个函数 运行 它无法识别字段对象的类型并下降!
一种解决方案是删除 Collection,如果您的 Collection 太重要,您可以编写一个脚本来删除旧对象并从中创建一个新实例。

是的,我还从字段属性中删除了 unique=True,然后又删除了 Collection 和 运行 代码。这解决了问题。