【问题标题】:cannot create unique index over {EDs._id: 1 } with shard key pattern { _id: "hashed" }无法使用分片键模式 { _id: "hashed" } 在 {EDs._id: 1 } 上创建唯一索引
【发布时间】:2019-05-04 02:44:50
【问题描述】:

我正在尝试索引主集合使用散列分片的嵌入式文档。 样本结构:

class ED(EmbeddedDocument):
    id = StringField(primary_key=true)
    meta = {
        'indexes': [{'fields': ['EDs.id'], 'unique': True}]
    }

class D(Document):
    id = StringField(primary_key=true)
    EDs = EmbeddedDocumentListField(ED)

现在,我在集合 D 中添加了一个分片。

sh.shardCollection("db.d", { "_id" : "hashed" } )

现在,由于我在 ED 的 id 上添加了索引,因此我无法保存文档。

$cmd failed: cannot create unique index over { EDs._id: 1 } with shard key pattern { _id: "hashed" }

但是,当我们从 ED 中删除此元数据时,它会起作用。当我删除分片时它也有效。 那么,我们如何才能在嵌入式文档 ED 的 id 上实现这种索引呢?

【问题讨论】:

  • 您可能应该提供更多信息。我什至不认识代码的类型。对于有关 MongoDB 的问题,shell 语法和 BSON 文档提高了获得有用答案的可能性。
  • 这是 Python 吗?一些上下文会有所帮助

标签: mongodb


【解决方案1】:

我能够通过将分片键实际包含在唯一索引本身中来使其工作。

【讨论】:

    【解决方案2】:

    createIndex 的“唯一”选项,在 MongoDB 的文献中说:“该选项不适用于散列索引” createIndex Options

    所以如果你在创建索引的时候去掉选项:{'unique': True},就不会再出现这个错误了。

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多