Unique Partial Index for a Subset of Enum Values

Hello everyone. I am working on a personal portfolio project. In my project I will have a collection that uses the polymorphic pattern, and each document represents a type of content that is in my portfolio (such as resume, headshot photo, contact information, and projects). Each document in the collection will contain a field that can be one of a set of enum values in order to differentiate the types of content in the collection. However, for a certain subset of these types, they can only occur once (such as resume, and personal photo), meaning that only one document in the collection can have that value. I was wondering if it would be a good practice to implement this by creating a unique, partial index on this subset of enum values.

Hi @Thomas_Jollota,

Based on what you shared, using a Partial Unique Index is a valid solution for your case. If you would like to see an example of a partial unique index in practice and some quirks of its implementation, I wrote an article about it, Unique Indexes Quirks and Unique Documents in an Array of Documents

Hello @Artur_57972 ,

Thanks for your very helpful reply.

One concern I have been wondering about is if it is okay for an partial, unique index to only contain a small handful of documents, by design. This would be because the index would only contain one document representing each of the enums (representing different content types) that must only appear once in the collection, and there is a only set number of these. In other words, there would only be a specific, handful number of documents in the index representing the subset of unique, singular types of content. (For example, the index may only ever have three documents - one for each of the following singular content types: resume, personal photo, personal introduction.) I do not know if this would be a good use of indexes/considered good practice. I would appreciate any help with getting clarity about this.

Have a great day everyone!