Any good suggestions on maintaining multiple unique fields within a sharded collection?

Hi all

As we know currently from the MongoDB documentation, if you have sharded collection and you want a specific field to be a unique index AND also have a shard key, the constraint is that shard key has to be this unique index’s prefix.

One of the workarounds around this when you need a unique requirement on a specific field in the collection is to set up a proxy collection like described in Enforce Unique Keys for Sharded Collections — MongoDB Manual 3.2

This means that for each unique field you want in the collection you will need a separate proxy collection, which is costly.

Are there any other workarounds?

Thanks

Hi @Mavericks2022
Welcome to the community!!

Unfortunately due to the way sharded collection works, the unique index must contain the full shard key as a prefix of the index. Using a proxy collection is the only supported solution at this moment.

However, there is a feature request to allow the exact scenario in https://jira.mongodb.org/browse/SERVER-19860 and also a related feedback item here that may be of interest to your use case: Unique index in sharded cluster – MongoDB Feedback Engine.

Thanks
Aasawari

2 Likes

Thanks @Aasawari , is there an estimate when MongoDB will be able to add the feature to support multiple unique field constraints?

e.g. upcoming ver6 vs a few years down the line vs etc

Hi @Mavericks2022

Unfortunately, we cannot predict when the functionality will be ready. You might, however, keep an eye on the aforementioned server ticket for updates.

Thanks
Aasawari

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.