Mongodb 复合唯一约束mongoengine不使用replicaset

Mongodb 复合唯一约束mongoengine不使用replicaset,mongodb,flask,pymongo,mongoengine,replicaset,Mongodb,Flask,Pymongo,Mongoengine,Replicaset,我试图使用unique_with param对多个属性强制执行唯一约束 ContentType = ('category', 'product') PageType = ('category', 'product', 'compare', 'review') class Content(db.Document): content_type = db.StringField(required=True, choices=ContentType) content_id = db.

我试图使用unique_with param对多个属性强制执行唯一约束

ContentType = ('category', 'product')
PageType = ('category', 'product', 'compare', 'review')


class Content(db.Document):
    content_type = db.StringField(required=True, choices=ContentType)
    content_id = db.StringField(required=True)
    page_type = db.StringField(required=True, choices=PageType, unique_with=['content_type', 'content_id'])
    title = db.StringField()
    description = db.StringField()
    keywords = db.ListField(db.StringField())
    footer = db.StringField()

    def save(self, *args, **kwargs):
        super(Content, self).save(*args, **kwargs)
这将在本地开发机器上创建索引。但无法在生产上创建索引,除了在_id上的默认唯一索引

在开发环境中,我使用一个mongo实例,而在生产环境中,我使用一个由3个实例组成的复制集(1个主、1个从和1个仲裁器)

我还尝试通过以下代码手动创建索引:

PageType = ('category', 'product', 'compare', 'review')
ContentType = ('category', 'product')

class Content(db.Document):
    content_type = db.StringField(required=True, choices=ContentType)
    content_id = db.StringField(required=True)
    page_type = db.StringField(required=True, choices=PageType)
    title = db.StringField()
    description = db.StringField()
    keywords = db.ListField(db.StringField())
    footer = db.StringField()
    meta = {
        'indexes': [
            'content_type',
            'content_id',
            'page_type',
            {
                'fields': ['content_type', 'content_id', 'page_type'],
                'unique': True
            }
        ]
    }

    def save(self, *args, **kwargs):
        super(Content, self).save(*args, **kwargs)
我不确定我是否在这里做错了什么,或者我的代码中有错误。任何帮助都会很好


还有一条信息:django应用程序正在使用相同的副本集,它能够创建复合索引。因此,我认为副本集配置没有问题。

您说无法在生产环境中创建索引?为什么不呢?你有什么错误?什么反应?服务器上的日志中有什么?可以通过连接到同一服务器的mongo shell创建索引吗?可以!我可以使用连接到同一服务器的MongoShell创建索引。我还没有检查生产日志。复制集不太可能有问题-在独立和复制集上创建索引没有什么不同。您是否能够从应用程序写入副本?是否打开了身份验证?可能您没有正确连接到副本集?我可以创建数据库并在副本集上写入和读取数据。其他一切都按预期进行。但索引不是通过上述任何方法创建的。应用程序当前未使用身份验证。所以这不是问题所在。你如何在前后检查索引的存在?我想知道它是否能在单机版上正常工作