2

I have tried following the instructions from the Microsoft Docs to create a unique index on a partitioned collection:

db.runCommand({shardCollection: db.coll._fullName, key: { university: "hashed"}});

db.coll.createIndex( { "student_id" : 1, "university" : 1 }, {unique:true})

The collection is created, however the createIndex command always returns the following error:

The unique index cannot be modified. To change the unique index, remove the collection and re-create a new one.

I have, using the emulator, tried this on databases with provisioned throughput on and off with the same result .

Has anyone been able to do this?

Sam
  • 85
  • 7
  • 2
    Have you enabled the latest MongoDB protocol (which can be found in the 'preview features' blade)? You probably need to do that, to support compound indexes. Have you tried with a single-property index? Note: I was successfully able to execute the commands you shared, with a newly-created MongoDB database+collection. – David Makogon Dec 11 '19 at 17:10
  • As below, the issue is the emulator doesn't recognise the command – Sam Dec 16 '19 at 16:46

1 Answers1

3

Follow @David's words, i also tested your commands successfully. Based on the statement in the document:

Currently, unique indexes can be created only when the collection is empty (contains no documents).

You could check whether your collection is empty.Another point is that maybe sometimes emulator have different behaviors with could real version.So, you could try to execute commands with official db.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32