Docs Menu
Docs Home
/ /

Create a Unique Index on Existing Sharded Collection

You can create a new unique index on a collection that is already sharded, but only if the index includes the shard key as a prefix.

The uniqueness constraint on an index ensures that documents in the collection have a unique value for the indexed field. For a sharded collection, MongoDB enforces uniqueness only when the index is the shard key or includes the shard key as a prefix.

For an already-sharded collection, you cannot create a unique index on other fields unless the shard key is included as the prefix. If the index doesn't include the shard key as a prefix, MongoDB returns an error when you run the createIndexes command.

To learn how to maintain uniqueness on a field that is not part of the shard key, see Unique Constraints on Arbitrary Fields.

  • Identify the shard key for the collection. To view the shard key, run the sh.status() method and locate the collection in the output.

  • Confirm that no existing documents violate the uniqueness constraint for the field. If a shard contains documents with duplicate values, the create index operation may succeed on the shards without duplicates but fail on the shard with duplicates, leaving inconsistent indexes across shards.

The following example creates a unique index on the email field of the accounts.names collection, which is sharded on the region_id field.

1

Run the sh.status() method and locate the accounts.names collection in the output to confirm that the shard key is region_id:

sh.status()
2

Connect mongosh to a mongos instance and create the unique index. Include the shard key region_id as a prefix of the compound index:

db.names.createIndex(
{ region_id: 1, email: 1 },
{ unique: true }
)
region_id_1_email_1

Back

Sharded Collections

On this page