Kumar Nitin
Kumar Nitin

Reputation: 1863

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

I'm trying to index the embedded documents where the main collection is using the hash sharding. Sample Structure:

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)

Now, I've added a sharding in collection D.

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

Now since I've added indexes on ED's id, it is not allowing me to save the document.

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

However, when we remove this meta from ED it works. It also works when I remove sharding. So, how can we achive this indexing on embedded document ED's id?

Upvotes: 4

Views: 4048

Answers (2)

Dan
Dan

Reputation: 932

I was able to get this to work by actually including the shard key in the unique index itself.

Upvotes: 4

MauriRamone
MauriRamone

Reputation: 503

'Unique' option for createIndex, in the literature of MongoDB says: "The option is unavailable for hashed indexes" createIndex Options

So if you remove the option: {'unique': True} when you create the index, you will not get this error anymore.

Upvotes: 3

Related Questions