Can we add new field as a unique field for existing collections?

can we add new field as a unique field for existing collections?
Does unique validation work?

Do you want to

add a unique index on an existing field?

or

add a field for which a unique index exists?

It works in both.

If a unique index exist and add a document with a duplicate value or modify a document with a duplicate value the operation will fail. If you try to create a unique index and duplicates exist the index creation will fail.

1 Like

It sounds like you have a collection where a certain field does not yet exist. You can add a sparse index that’s unique which will enforce uniqueness across all the documents which have this field but will ignore documents which don’t have this field.

Does this help?

Asya

2 Likes

Yes, thank you; this was helpful.

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